histogram on data graph
2 次查看(过去 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
编辑:Eli
2024-3-29
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
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
移动:Star Strider
2024-3-29
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
编辑:Star Strider
2024-3-29
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
编辑:Star Strider
2024-3-30
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 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Spreadsheets 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 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 (한국어)