load carbig boxplot(MPG,Cylinders)
I will reformat the MPG vector into a 2D array for column indexing.
c = unique(Cylinders); mpg = nan(numel(MPG),numel(c)); for i=1:numel(MPG) mpg(i,Cylinders(i)==c) = MPG(i); end
Then I can use distributionPlot to make a two-dimensional histogram (see also hist) for comparison. I like heat maps so I'll choose just gray levels.
Notice how I got some other behaviors for free. I intended to use "hold on" to overlay both types of plots but Jonas saved me a little work. He also chose to draw histogram patches behind my boxplot so I didn't even have to worry about call order. I did not expect either, but I like both.
Check out his screen shots for more variations such as making the widths of each histogram bin vary with density. Thanks, Jonas!
Get the MATLAB code
Published with MATLAB® 7.10
2 CommentsOldest to Newest
Nice plots with good statistical overview.
Now, not to be a total ass, but on the topic of presenting data in easy-to-grasp form, you might want to label your axis….
Daniel, thanks for your comments.
I agree that axes labels are value added, and I generally add them as a rule. For this example, however, I consciously did not add them. (Had I seen a way to avoid the data massaging code I would have skipped that too.) I hope their absence did not cause too much confusion or anxiety.