現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
histogram on data graph
39 ビュー (過去 30 日間)
古いコメントを表示
Hi,
I have a graph i plot as
plot(x,y). it looks like this:
I want to make it a histogram like this:
both the inner colors as you see correspond to the histogram, and also the histogram on the right side of each graph.
How can i do it?
採用された回答
Star Strider
2024 年 3 月 24 日
One option (if your data were a scatter plot) would be the scatterhist or scatterhistogram functions.
Otherwise, perhaps something like this —
x = linspace(0, 100, 1000);
y = randn(size(x));
pd = fitdist(y(:), 'normal');
xd = linspace(min(y), max(y), 250);
yd = pdf(pd, xd);
figure
tiledlayout(1,3, 'TileSpacing','none')
nexttile([1 2])
plot(x, y)
nexttile
plot(yd,xd)
Ax3 = gca;
Ax3.XTick = [];
Ax3.YTick = [];
Experiment to get the result you want.
.
14 件のコメント
Star Strider
2024 年 3 月 24 日
My pleasure!
You didn’t provide the data you’re using (other than an example that looks what I plotted), so I don’t know what you want. There is no actual histogram (it is a probability density distribution function), and I don’t know what the different colours are in the example, or how that was plotted. You can certainly specify a colormap and provide a colorbar (that with a bit of effort you can place anywhere). I also don’t know what the distribution funciton is supposed to represent.
Two examples —
x = linspace(0, 100, 1000);
y = randn(size(x));
z = randn(size(x))/5;
pd = fitdist(y(:), 'normal');
xd = linspace(min(y), max(y), 250);
yd = pdf(pd, xd);
figure
tiledlayout(1,4, 'TileSpacing','none')
nexttile([1 3])
plot3(x, y, y)
hold on
plot3(x, z, z+2)
hold off
view(0,90)
colormap(turbo)
colorbar('Location','south')
Ax1 = gca;
Ax1.Box = 'on';
nexttile
plot(yd,xd)
Ax3 = gca;
Ax3.XTick = [];
Ax3.YTick = [];
pd = fitdist(z(:), 'normal');
xd = linspace(min(y), max(y), 250);
yd = pdf(pd, xd);
figure
tiledlayout(1,4, 'TileSpacing','none')
nexttile([1 3])
plot3(x, y, y)
hold on
plot3(x, z, z+2)
hold off
view(0,90)
colormap(turbo)
colorbar('Location','south')
Ax1 = gca;
Ax1.Box = 'on';
nexttile
plot(yd,xd)
Ax3 = gca;
Ax3.XTick = [];
Ax3.YTick = [];
Experiment with these to get the result you want.
.
Eli
2024 年 3 月 24 日
by colors in the graph, this means how may times each point appers. it needs to be relevant to the histogram shown. as you can see the color bar is marked as the number of counts.
Star Strider
2024 年 3 月 24 日
編集済み: Star Strider
2024 年 3 月 25 日
I have no idea what your data are (you haven’t provided them) or what the probability distribution is supposed to depict.
In the absence of provided data and a comprehensive description of what the plot is supposed to represent, I’m doing the best I can to guess.
EDIT — (25 Mar 2024 at 00:59)
Also, the probability distribution function is calculated from the data in the plot to the left of it. That appears to be exactly what you requested.
Eli
2024 年 3 月 25 日
here is an exmple:
randomInts = randi([0, 100], 1, 10000);
randomNumbers = randomInts / 10;
if i do plot(randomNumbers) i'll have the normal plot.
But what i want is that the plot will be with colors according to the histogram. meaning the numbers the closes to the most counts (middle of the histogram) will have a specific color that will change colors as it goes away from the hostogram. Just as in the second photo in the original question.
Star Strider
2024 年 3 月 25 日
It is difficult for me to simulate this, because I have no idea what the actual data are, or what that plot actually represents.
Try this —
x = linspace(0, 1E+2, 1000); % Create 'X' Data
y = randn(size(x)); % Create 'Y' Data
z = randn(size(x))/2.5; % Used To Calculate & Plot Distribution
pd = fitdist(z(:), 'normal'); % Calculate (Fit) 'z' Distribution
xd = linspace(min(y), max(y), 250); % Distribution Independent Variable For Plot
yd = pdf(pd, xd); % Generate PDF Of Distribution
z = pdf(pd, y); % Scale 'z' To Match Distribution
figure
tiledlayout(1,5, 'TileSpacing','none')
nexttile([1 4])
scatter(x, y, 15, z, 'filled' ) % Plot Data As 'scatter', Use 'z' To Define Colours
colormap(turbo)
colorbar('Location','south') % Plot 'colorbar'
Ax1 = gca;
Ax1.Box = 'on';
nexttile
plot(yd,xd) % Plot Distribution
Ax3 = gca;
Ax3.XTick = [];
Ax3.YTick = [];
This takes considerable liberties with the data, however the plots seem to be appropriate. In any event, this is as close as I can get to the second plot image. Make appropriate changes in it to get the result you want. I cannot get the data any denser here, since that may be related to limits imposed on running it here (to limit memory use). I cannot determine how this will work with your actual data, since I do not have it to work with.
.
Eli
2024 年 3 月 25 日
編集済み: Eli
2024 年 3 月 25 日
Hi
I'm sorry if i wasn't clear.
I don't have z.
only Y and number of measurment.
The distrabution doesn't have to be normal. it can be whathever happens. maybe i have for example sample that goes up with time and fluctuated.
The scale of the histogram should represent how many points (counts) there are in specific y value
and so is the color of each point in the plot.
randomInts = randi([0, 100], 1, 10000);
y = randomInts / 10; %value
x=1:length(y); %measurement number
figure();
plot(x,y)
%in this example, it's not a gaussian:
hist(y)
Star Strider
2024 年 3 月 25 日
‘The scale of the histogram should represent how many points (counts) there are in specific y value
and so is the color of each point in the plot.’
My latest version does exactly that —
x = linspace(0, 1E+2, 1000); % Create 'X' Data
y = randn(size(x)); % Create 'Y' Data
z = y/2.5; % Used To Calculate & Plot Distribution
pd = fitdist(z(:), 'normal'); % Calculate (Fit) 'z' Distribution
xd = linspace(min(y), max(y), 250); % Distribution Independent Variable For Plot
yd = pdf(pd, xd); % Generate PDF Of Distribution
zv = pdf(pd, y); % Scale 'z' To Match Distribution
figure
tiledlayout(1,5, 'TileSpacing','none')
nexttile([1 4])
scatter(x, y, 15, zv, 'filled' ) % Plot Data As 'scatter', Use 'z' To Define Colours
colormap(turbo)
colorbar('Location','south') % Plot 'colorbar'
Ax1 = gca;
Ax1.Box = 'on';
nexttile
plot(yd,xd) % Plot Distribution
Ax3 = gca;
Ax3.XTick = [];
Ax3.YTick = [];
With respect to the distribution, it looks like it s most likely normal (and also appeared that way in the plot image). If you suspect that it could be distributed differently (for example lognormal, Poisson, or others), you can test different distributions to see what fits best, then use that in the fitdist call. The rest of my code wiuld likely not change, since the pdf call and the statements using it would automatically adapt.
If you want to share your data, please feel free to do so. Otherwise,it does not appear to me that anything further is required here. I have done the best I can, given the information I have.
.
Eli
2024 年 3 月 29 日 5:45
編集済み: Eli
2024 年 3 月 29 日 6:01
Hi
So i found out those graphs were actually made in python using
hist2d.
They choose different segments of bins and than it's possible to see the moving of the set points. in matlab i assume, it's possibut to make it with this?
basically it's looking how the peak power is moving and how it all changes in time. we can see also that the color becomes more red (in the original question) as the spread becomes narrower.
Star Strider
2024 年 3 月 29 日 10:32
I do not see how histogram2 would help with this. It could be put in the last tiledlayout plot, however it might need to be rotated and that could destroy its utility. A better option could be to use the stairs function (or stairs option) with a histogram call if that is what you want.
Consider the other two options I linked to earlier, specifically scatterhist or scatterhistogram if you are not satisfied with the code I came up with, or use the python function. I have never used python, so I leave that to you.
Eli
2024 年 3 月 29 日 16:57
移動済み: Star Strider
2024 年 3 月 29 日 17:22
in the end i used this:
pd = fitdist(y(:), 'normal');
xd = linspace(min(y), max(y));
yd = pdf(pd, xd);
h=figure();
tiledlayout(1,3, 'TileSpacing','none')
nexttile([1 2])
histogram2(xdata,ydata,[100 23],'DisplayStyle','tile','LineStyle','none','ShowEmptyBins','off');
grid off
ylim([1.85 1.895])
colormap("jet")
nexttile
plot(yd,xd)
Ax3 = gca;
Ax3.XTick = [];
Ax3.YTick = [];
ylim([1.85 1.895])
I got the attached figure.
For some reason two things are an issue for me and maybe i should open a new thread about it (?)
- the following code doesn't work (position error)
ax = gca;
ax.Units = 'centimeters';
pos = get(ax,'pos');
ax.Position = [pos(1)+2 pos(2)+2 8 4]; % hh1.Position = [p
ax.FontSize = 13;
ax.TickDir = 'in';
ax.TickLength = [0.02 0.02];
ax.XMinorTick = 'off';
ax.YMinorTick = 'off';
ax.LineWidth = 1;
ax.Box = 'on';
set(gca, 'Layer','top');
set(gca, 'FontName', 'Arial');
2. I want to save it in SVG format and design using InkSpace app. for some reason I can't choose anything from the photo in inkspace and i can't understand why.
Star Strider
2024 年 3 月 29 日 17:21
編集済み: Star Strider
2024 年 3 月 29 日 20:13
This looks quite similar to what I created. I wasn’t certain how you wanted to use histogram2, since you weren’t explicit. I thought you wanted to use it for the right normal distribution plot.
I don’t know what the second code refers to, so I can’t troubleshoot it. Note that tiledlayout plots are resistant to having their positions adjusted, and have made it essentially impossible to do so. (This is not normally a problem, since using tiledlayout figures and their attributes is usually the point.)
F = openfig('power_new.fig');
I have nothing further to add at this point. I never got your data, or a description of what you wanted to do with it, so that definitely limited my ability to respond to your original question.
EDIT — (29 Mar 2024 at 20:13)
I can’t find anything in an Interweb search for for ‘InkSpace’, however there are several entries for ‘Inkscape’. MATLAB offers the option '-dsvg' (no direct link) listed in the formattype documentation section for the print function. There are also options for encapsulated PostScript formats.
.
Star Strider
2024 年 3 月 30 日 2:15
編集済み: Star Strider
2024 年 3 月 30 日 10:32
O.K.
I don’t have any experience with vector graphics. It’s never been something I needed to understand.
I just knew that the print function can save graphics objects in a variety of formats, so I suggested it.
Another option that I jjust now thought of is the exportgraphics function. (Again, I have no experience with it.)
EDIT — (30 Mar 2024 at 10:32)
I’m glad you found a solution. It’s difficult to help you because you never share your code or data, so I’m left guessing. I won’t from here on. Good luck with your projects!
.
その他の回答 (0 件)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
アジア太平洋地域
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)