이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
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!오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 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 (한국어)