Building colourmap from continuous x, y position data

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 日

0 投票

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.

1 件のコメント

Aishwarya Nair
Aishwarya Nair 2023 年 8 月 3 日
編集済み: Aishwarya Nair 2023 年 8 月 3 日
Thank you!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeData Distribution Plots についてさらに検索

質問済み:

2023 年 3 月 27 日

編集済み:

2023 年 8 月 3 日

Community Treasure Hunt

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

Start Hunting!

Translated by