MATLAB Answers

Scatterhist function not allowing for boxplot on the y-axis to stay

5 views (last 30 days)
Saar Peles
Saar Peles on 16 Oct 2020
Commented: Adam Danz on 27 Oct 2020
Hi I'm trying to run a scatterhistogram (scatterhist()) but whenever I run the function the figure will pop up with the boxplot correctly displayed for a brief second on the y-axis and then disappear immediately. I'm not sure why. I also want to be able to color in the points and I'm not sure how to do that. Any help will be appreciated.
k = scatterhist(swine2, ready.rr_la, 'Group', swine);
hold on;
j = get(k(1),'colororder');
boxplot(k(3),ready.rr_la,swine2, 'Labels', {'10','11', '12', '13'}, 'color', j);
The figure displayed is atttached


Show 2 older comments
Adam Danz
Adam Danz on 23 Oct 2020
Please provide a minimal working example that demonstrates the problem.
When I add a boxplot to a scatterhis, it works fine.
Saar Peles
Saar Peles on 23 Oct 2020
Sure, when adding all the view functions suggested this is what I get. When it first loads, the boxplot pops up but then immediatedly disappears.
Adam Danz
Adam Danz on 23 Oct 2020
That's helpful but it's not a "MWE" (something we can copy-paste and run on our end to reproduce the problem).
It looks like you're not specifying the axes. I'll add an answer but if it doesn't solve the problem you'll need to give a MWE.

Sign in to comment.

Answers (1)

Adam Danz
Adam Danz on 23 Oct 2020
Edited: Adam Danz on 23 Oct 2020
It looks like you're trying to add a boxplot to the y-historgram axes. Recall that the y-histogram in scatterhist is reversed. Also, you're rotating the scatter axes so that x and y are switched but the histograms aren't switched. Why not just switch x and y in the scatterhist function?
h = scatterhist(y,x,___);
To add a boxplot to the y-histogram axis,
hold(h(3), 'on')
boxplot(h(3), ___)
Use h(2) for the x-histogram or h(1) for the scatter axes.


Show 1 older comment
Adam Danz
Adam Danz on 23 Oct 2020
Thanks, that made things easy.
Problem #1: The axis handle needs to be the first input
% boxplot(ready.rr_la, swine2, k(3), 'labels', {'10','11', '12', '13'}, 'color', j);
% wrong spot ^^^^
Problem #2: The bigger problem; note the vertical axis limit in the originl data. I added axis(k(3),'on') to see th y-histogram axis ticks. Note that the vertical axis is the "X" axis and the horizontal axis is the y. It may be helpful to add ylabel(k(3),'y'); xlabel(k(3),'x'). Also note that the y-ticks in the histogram match the y-ticks in the scatter plot; same with the x-ticks for the lower histrogram axes. You'll want to maintain this relationship, otherwise the histograms are useless and misleading.
Now add the boxplots,
The origina data are still there off the axes. You can see them if you change the axis limits but now the axes are useless and no longer align with the scatter plot.
xlim(k(3),[-.2 5])
ylim(k(3),[-.2, 15])
So, what to do about this!? Well, I don't understand the point of the boxplots being there in the first place. If they relate to the histogram, you'll need to normalize them so they fit on top of the hist rather than somehwere else in the axes. Recall from the documentation, "By default, scatterhist finds the optimal bandwidth for estimating normal densities".
Saar Peles
Saar Peles on 23 Oct 2020
I appreciate you taking the time to do this, but how would I put the boxplots onto the data. I tried doing it with the example you sent in my other post, however the erro it gave me was strange to say the least
Adam Danz
Adam Danz on 27 Oct 2020
I don't understand how your boxplots are supposed to map onto the histograms. But their x & y values need to be within range of the histogram's x & y values.
That will be very hard to do.

Sign in to comment.




Community Treasure Hunt

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

Start Hunting!