Scatterhist without normalized y axes on the marginal histograms. How ?

I am trying to plot some data using the scatterhist function.
The plotting works perfectly if not for one thing: when plotting data using a grouping variable the marginal histograms look all normalized to their own maxima.
This makes it impossible to compare the relative abundance of elements in different groups, as it just gives an idea of how the data are distributed within each of them.
I would like instead to conserve the former information, thus allowing group comparisons.
It should be possible to access the handles of the marginal histograms in this way.
If "s" is the general handle: s = scatterhist(x,y,'Group',grouping_variable,'Kernel','on').
s(1) is the handle for the scatter plot
s(2) the bottom marginal hisogram (referred to the X axis) and
s(3) the one for the vertical histogram (referred to the Y axis)
I also did not manage to change the bin size of the two.
Perhaps all this is very trivial...
thanks for your help

 Accepted Answer

" when plotting data using a grouping variable the marginal histograms look all normalized to their own maxima"
That shouldn't be the case. Consider this example. Note that each group's marginal histograms have different maximum heights which shows that they are not normalized to their hown maxima, if I understand you correctly.
The axes containing the maginal histograms are normallized (axis limits are [0,1]) when you're plotting kernel density but that doesn't affect the shape of the distribution. See comment below to learn how to turn the histogram axes on conver them from normalized axes to axes that show bin counts.
load fisheriris.mat;
x = meas(:,1);
y = meas(:,2);
clf()
scatterhist(x,y,'Group',species,'Kernel','on')

5 Comments

Thanks for the answer. However, when I look at my plot I get a high peak if one group has 1 element.
The X axis scale is logaritmic (I forgot to mention), but this should not affect the histogram (at least it doesn't in a normal hist plot).
That is why I have concluded the values are normal.....I hoped to find a command to set the normalization OFF somehow....
in the plot above I have indicated the totals next to each "histogram-curve" as an example. the same would occur if I use the normal "built in" histogram plot
The image and your explanation help to understand your query a little better.
If you look at the y axis limits of the marginal histograms, you'll see that they are, indeed, normalized.
ylim(s(2))
ylim(s(3))
Alternatively, you could turn the axes on for this histogram plots.
axis(s(2:3),'on')
Also note that the y axis isn't plotted so if the historgrams were not normalized, they would still look the same way.
" However, when I look at my plot I get a high peak if one group has 1 element"
I don't see that in the image you shared. If the point is the only point on the axis, then you would expect a high peak at that point. If there is another group of data that has 10 points concentrated in the same area, you'd expect the peak of that distribution to be ~10x larger. However, if there are 10 data points exactly on the same coordinate, you would only see 1 point but the histrogram's peak would represent all 10 points.
"I hoped to find a command to set the normalization OFF somehow...."
Again, it's unlcear what you would expect. The shapes of the curves/histograms wouldn't change. Only the y axis scaling would.
One option is to replace the histogram with custom histograms. Here's a demo.
load fisheriris.mat;
x = meas(:,1);
y = meas(:,2);
clf()
h = scatterhist(x,y,'Group',species);
cla(h(2))
g = findgroups(species);
hold(h(2),'on')
arrayfun(@(i)histogram(h(2),x(g==i),'BinWidth',0.25),unique(g))
cla(h(3))
hold(h(3),'on')
arrayfun(@(i)histogram(h(3),y(g==i),'BinWidth',0.25),unique(g))
axis(h(2:3),'tight')
axis(h(2:3), 'on')
thanks but somehow I didn't get it to work.
one plot gets deleted, the other doesn't and no new plots are drawn
Are you able to run my code and produce the demo figure I shared?
Have you substituted your data correctly into the code from my demo?
If you show the relevant section of code, I can take a quick look at it. But first, confirmt the first question.
thanks it works, my grouping variable was wrongly set.
I was still wondering whether one could modify the normalization of the existing plot (just to avoid adding lines of code) or in any case represent the histogram as a curve (which makes it easier to understand when several groups are overlapped).
I also have stil a question on the plotting. In order for the x axes to be aligned with the scatter plot I have to use the hand tool, drag the scatter plot "around", the plot will then "bounce back" into its original position and the histograms will now be aligned... it's a bit weird. It seems the plotting does not update unless one tries to manually translate the axes. Is this the case ?
Thanks anyway !

Sign in to comment.

More Answers (0)

Asked:

LO
on 27 Feb 2020

Edited:

on 17 Dec 2020

Community Treasure Hunt

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

Start Hunting!