Building colourmap from continuous x, y position data

43 views (last 30 days)
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?

Accepted Answer

Jack
Jack on 27 Mar 2023
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.

More Answers (0)

Categories

Find more on Colormaps in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!