MATLAB Answers

How to put a boxplot on a scatterplot/histogram?

16 views (last 30 days)
Saar Peles
Saar Peles on 22 Oct 2020
Edited: Adam Danz on 27 Oct 2020
I'm trying to put a boxplot over the data points in this scatterplot but can't find anything. This is the code to setup the table for the scatterhistogram
tbl = table(swine, cc, depth);
s = scatterhistogram(tbl, 'swine', 'cc', 'GroupVariable', 'depth');
The scatterplot is attached. Many thanks

  5 Comments

Show 2 older comments
dpb
dpb on 23 Oct 2020
Ah, yes...another case of TMW getting far too clever for their own good. :(
Adam Danz
Adam Danz on 23 Oct 2020
Yeah, I'm not sure I understand the reasoning behind the family of high-level, standalone visualizations such as scatterhistogram, that mirror other visualization functions but without the same level of customizability.
To make things even more confusing, there are inconsistencies in the param names between the two functions (ie, nbins vs NumBins, which appear to do the same thing).
TMW have made some improvements to the standalone functions, though. I submitted a feature request earlier this year because scatterhistogram wouldn't allow us to add anything to the axes such as yline(),text(), or even a marker to indicate mean/median values using plot(). boxchart() is another example of a standalone version of boxplot() but I recall being able to make these modifications in boxchart. It looks like theses capabilities have been added to scatterhistogram in the r2020b release.
I'm guessing that their motovation is to streamline the use of these functions without the need to manipulate handle properties but if you compare the inputs between the standalone versions and the customizable versions, they aren't that much different. The standalone versons also give TMW greater flexibility in updating the functions without worrying about backward compatibility associated with external property manipulation. Except for the most basic use cases, I bet a lot of newer users eventually migrate their visualization from the standalone versions to the customizable versions as in this thread.
dpb
dpb on 24 Oct 2020
Yeah, seems like they waste a LOT of time on such trivia and neglect far more significant improvements. Meanwhile the code bloat just continues to mutiply.
It certainly is not clear who's driving the ship and where they think they're going.

Sign in to comment.

Answers (1)

Adam Danz
Adam Danz on 23 Oct 2020
Edited: Adam Danz on 27 Oct 2020
scatterhistogram() is its own class and does not provide access to its axes. Instead, use the older scatterhist(). It's output is a 1x3 vector of axis handles where the first handle is the axis to the scatter plot.
h = scatterhist(___);
hold(h(1), 'on')
boxplot(___)
However, the marginal histograms are normalized and the boxplots will likely be at a much larger scale than the histograms. To see the axis scale, turn on the axes using,
axis(h(3), 'on')
I suggest putting the boxplots elsewhere, perhaps in their own axes or within the scatter axes.

  3 Comments

Saar Peles
Saar Peles on 23 Oct 2020
Is it possible to also just put points onto a boxplot? Maybe that would be easier

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!