How to plot a histogram along a curve

10 vues (au cours des 30 derniers jours)
Daniel Blatter
Daniel Blatter le 24 Jan 2020
Réponse apportée : dpb le 29 Jan 2020
Hi,
I have (x,y) data that lie along a curve whose form I do not know analytically. I want to plot a histogram (or estimated PDF) of the data along this curve, and have the color of the curve indicate the bin count (or probability density). Using histogram2 I can produce something like the following, but I would much rather have the curve (shown here in black) indicate the probability density rather than these clunky 2D bins that have to be either above or below the curve at any point. Mostly this is because I want to overlay this PDF on top of another figure, but the only way I know how is to use a contour plot of q.Values, and this looks clunky and isn't very accurate, actually.
Does anyone know of a way to plot a PDF along a curve?
Thanks!
Dan
q = histogram2(x,y,30,'Normalization','pdf','DisplayStyle','tile','ShowEmptyBins','off','EdgeAlpha',0.0);
mFx_meltH2O_PDFcurve.png
  4 commentaires
dpb
dpb le 26 Jan 2020
Yes. One can compute/estimate an empirical cdf/pdf, and compare to a theoretical or other set of data, and maybe that's what's intended, but it's certainly not clear from the description as provided and w/o the data to look at and some background as to what it is we are looking at, it's imponderable..
Daniel Blatter
Daniel Blatter le 27 Jan 2020
Sorry for the confusion, I should have phrased the question more clearly. The (x,y) data are attached. All points plot along the thin black line shown in the original figure. Which means I should be able to make a PDF that is, effectively, 1D. But I want to plot it in 2D. In other words, the PDF is parametric: f(x,y) --> g(t). Only, it doesn't help me to plot the PDF as a 1D function of t, I want to see where the probability density lies in (x,y).
Maybe this plot (attached) will make it a little clearer. I made this plot by manipulating the opacity of scatter plot markers, not by properly calculating and normalizing a histogram.
f1 = scatter(meltH2OfromRho_(1:1e2:end),100*m_(1:1e2:end),'wo','filled','linewidth',0.001);
f1.MarkerFaceAlpha = 0.008;
Is there a proper statistical way of doing this?

Connectez-vous pour commenter.

Réponses (2)

dpb
dpb le 27 Jan 2020
Oh. That's much more understandable, thanks. Something like
histogram2(x.',y.','Normalization','pdf','FaceColor','flat')
colorbar
xlabel('X'),ylabel('Y')
maybe? Above generates the following after using the interactive rotation facility to get the particular perspective. Your data are so concentratred along the path there's not much disparity in the transverse direction...
untitled.jpg
  2 commentaires
Daniel Blatter
Daniel Blatter le 28 Jan 2020
Indeed, the lack of disparity in the transverse direction is kind of the whole point. Histogram2 for a points that literally lie on a line (a parametric curve) is overkill, and leads to "chunkiness" when viewed from top-down as a grid, since the curve passes through different portions of each 2D bin.
The suggestion you made is how I've been solving this so far, but I was hoping to be able to plot a curve whose color changed as probability changed, rather than using Histogram2's bins as an approximation to that curve. Does that make any sense?
Many thanks for you time and help :)
dpb
dpb le 29 Jan 2020
"to be able to plot a curve whose color changed as probability changed" is, unfortunately, not possible with a line as the line color is a single property of the line in a 2D axis, unfortunately. You could create line segments and set their color similar to what you've done before w/ scatter.

Connectez-vous pour commenter.


dpb
dpb le 29 Jan 2020
Well, your data is rather unique (so to speak :) ): I was trying to figure out how you did the opacity thing in knowing the relative frequency so I poked around some and discovered the interesting factoid:
>> ux=unique(x); uy=unique(y);
>> whos u*
Name Size Bytes Class Attributes
ux 1x562 4496 double
uy 1x562 4496 double
>>
That's most interesting, indeed!!! So,
>> n=histc(y,uy); % get a count for each unique; assume (didn't check) x,y go together
In that case, then one can create the illusion of a line with variable color as:
hS=scatter(ux,uy,3,n,'filled');
that results in:
untitled.jpg
One may need to poke around with color maps to make the range more apparent...
Otherwise, I'm pretty-much out of ideas...

Community Treasure Hunt

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

Start Hunting!

Translated by