Can I make a 1D heatmap of data density and add it to line charts?

8 次查看(过去 30 天)
I have 7 subplots of line graphs with 10 time-points on the x-axis and a y-axis scaled from -200 to 200. There are 36 series of data in each graph, all of which begin at 0,0 and fan out to varying degrees above, below and along the horizontal (see attached image for two of the graphs). I would like to create a vertical line heatmap of the distribution of data in the final time-point and display it at the right-hand edge of each graph. I see it as a colour-based histogram for the last time-point scaled from lowest to highest count per bin.
I have used histcounts to count the data into 6 bins and get the edges of the bins for each dataset, but I do not know how to create the bar. I don't know if this is the right approach, or if it's possible in Matlab.
Can anyone help?
Thanks,
Michael.

回答(1 个)

Paras Gupta
Paras Gupta 2023-9-6
Hi Michael,
I understand that you are trying to create a histogram to show the distribution of the data in the final time-point and want to set the colors of the bars to give the effect of a heatmap but in one dimension. Please find below the code to achieve the same.
% Number of lines and data points
numLines = 36;
numPoints = 10;
% Generate random endpoints for each line
% endPoints = randi([-200, 200], numLines, 1);
endPoints = normrnd(0, 50, numLines, 1); % Adjust the standard deviation as needed
% Create x values
x = linspace(0, 1, numPoints);
% Plotting the lines
figure;
subplot(1, 2, 1);
hold on;
for i = 1:numLines
y = linspace(0, endPoints(i), numPoints);
plot(x, y);
end
hold off;
% Set axis labels and title
xlabel('X');
ylabel('Y');
title('36 Straight Lines');
numBins = 6;
[counts, edges] = histcounts(endPoints, numBins, 'BinLimits', [-200, 200]);
subplot(1, 2, 2);
% Create a heatmap-like effect using imagesc
imagesc(counts);
colormap('hot');
colorbar;
% Set the x-axis tick labels
xticks(1:numBins);
xticklabels(round(edges, 2));
xlabel('Ending Time-point');
% Set the y-axis label
ylabel('Frequency');
The above code creates a sample graph like the ones described in the question. The vertical line heatmap is created and placed to the right of the sample graph. You can refer to the following documentations for more information on the code:
Hope this helps.

类别

Help CenterFile Exchange 中查找有关 Data Distribution Plots 的更多信息

产品


版本

R2019a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by