Plot 2D-histogram for X and Y

275 ビュー (過去 30 日間)
BN
BN 2020 年 7 月 20 日
編集済み: Alessandro Maria Laspina 2022 年 7 月 20 日
Dear all,
I have two types of data sets (X and Y) with equal size, which I would like to plot 2D-histogram of them, in order to compare X by Y.
So the larger the scatter implies the greater
disagreement.
I used this script below:
data = [X,Y];
hist3(data,'CdataMode','auto')
xlabel('observed')
ylabel('modeled')
colorbar
view(2)
And here is my achievement:
Unfortunately, as you can see this plot does not represent my goal, for instance, please look at this figure below (I want to achieve a plot like this below):
So any suggestion is really helpful.
Thank you all
  3 件のコメント
BN
BN 2020 年 7 月 20 日
編集済み: BN 2020 年 7 月 20 日
Yes, as you said I want to have a plot like the bottom figure.
The extra description is:
Here the scatterplot of my X and Y data:
But I want to have a figure like the bottom image in my question (that i found it on google):
In order to know in each situation how many points exist.
Thank you so much.
Roger J
Roger J 2020 年 7 月 20 日
Try:
>> hist(X)
>> hist(Y)
I did, and it plotted each vector, and most(almost all) of your data is less than 50 for both X and Y. Seems like the histogram is correct for that data.

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

採用された回答

Star Strider
Star Strider 2020 年 7 月 20 日
Try this:
D1 = load('X.mat');
D2 = load('Y.mat');
X = D1.X;
Y = D2.Y;
data = [X,Y];
hh3 = hist3(data, 'Nbins',[1 1]*60);
figure
image(flipud(hh3))
ax = gca;
xt = ax.XTick;
yt = ax.YTick;
ax.XTickLabel = xt*10;
set(ax, 'YTick',[0 yt], 'YTickLabel', [flip([0 yt])]*10)
producing:
Experiment to get different results.
.
  3 件のコメント
Star Strider
Star Strider 2020 年 7 月 29 日
As always, my pleasure!
Alessandro Maria Laspina
Alessandro Maria Laspina 2022 年 7 月 20 日
編集済み: Alessandro Maria Laspina 2022 年 7 月 20 日
How would I do this but with log scales on the x and y axis (assuming no negative or 0 values)? If I use set(gca,'Yscale','log') it leaves a blank space

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

その他の回答 (3 件)

Cris LaPierre
Cris LaPierre 2020 年 7 月 20 日
編集済み: Cris LaPierre 2020 年 7 月 20 日
A couple issues to be aware of.
  1. You are using a different colormap. It looks like the goal image is using Jet.
  2. Your X and Y values are dominated by the counts in the first bin (histograms below). Consider using caxis to keep the colorbar focused on the desired range.
Try adding (and adjusting to meet your needs) the following code.
colormap("jet")
caxis([0,80])
  1 件のコメント
BN
BN 2020 年 7 月 29 日
Thank you so much +1

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


Image Analyst
Image Analyst 2020 年 7 月 20 日
Your data does not peak in the 100 to 250 range. It peaks around 0:
load('x.mat');
load('y.mat');
data = [X,Y];
h = histogram2(X, Y,100)
xlabel('observed')
ylabel('modeled')
% Set colormap, but it won't have any effect.
colormap(jet(256));
colorbar;
% view(2)
% Zoom in on the 0-200 range.
xlim([0,200]);
ylim([0,200]);
% Label the plot.
title('Counts', 'FontSize', 20);
xlabel('X', 'FontSize', 20);
ylabel('Y', 'FontSize', 20);
coefficients = polyfit(X, Y, 1);
xFit = xlim;
yFit = polyval(coefficients, xFit);
hold on;
plot3(xFit, yFit, [0,0], 'r-', 'LineWidth', 3);
c = corrcoef(X, Y)
This is essentially just what you saw, just that I used narrower bins and used a more modern function: histogram2(). Why do you think it's wrong and that you should have more counts in the 100-250 range?
  1 件のコメント
BN
BN 2020 年 7 月 29 日
編集済み: BN 2020 年 7 月 29 日
Yes you right, Thank you so much. +1

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


Steven Lord
Steven Lord 2020 年 7 月 20 日
In addition to histogram2 which Image Analyst suggested, take a look at the heatmap function. I think showing a heatmap of the data binned by histogram2 or histcounts2 will be pretty close to the picture you want.
  1 件のコメント
BN
BN 2020 年 7 月 29 日
Yest Thank you so much +1

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

カテゴリ

Help Center および File ExchangeData Distribution Plots についてさらに検索

タグ

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by