Building colourmap from continuous x, y position data

38 次查看(过去 30 天)
I want to create some sort of a heatmap using x,y position data. I want the axes of the figure to be x and y and the colour of the plotting to be more intense if there is more data around that position.
How can i achieve this?

采纳的回答

Jack
Jack 2023-3-27
Hi,
To create a heatmap using x and y position data in MATLAB, you can use the histcounts2 function to create a 2D histogram of the data, and then visualize the results using the imagesc function. Here's an example:
% Generate some example data
x = randn(1000,1); % x position data
y = randn(1000,1); % y position data
% Create a 2D histogram of the data
nbins = 50; % number of bins in each dimension
[counts, edges] = histcounts2(x, y, nbins);
% Visualize the results as a heatmap
figure
imagesc(edges(1), edges(2), counts)
colormap(jet) % choose a colormap
colorbar % add a colorbar to the plot
axis xy % set the axis orientation to x-y
xlabel('x') % label the x-axis
ylabel('y') % label the y-axis
In this example, we first generate some random x and y position data. Then we use histcounts2 to create a 2D histogram with a specified number of bins (nbins). We then use imagesc to visualize the results as a heatmap, using the edges of the bins as the axes. Finally, we set the colormap, add a colorbar, and label the axes.
The resulting plot will have a more intense color in regions where there is more data. You can adjust the number of bins (nbins) to control the level of detail in the heatmap.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Colormaps 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by