MATLAB Answers

Scatterhist: width of Boxplot

25 views (last 30 days)
Fabian Teichmann
Fabian Teichmann on 16 Apr 2021 at 15:00
Edited: Adam Danz on 19 Apr 2021 at 13:31
Hello,
ist it possible to change the width of the left boxplot in the following example taken from your website? I'd like the width of the left boxplot to be the same as the height boxplot below the scatter plot. Can anyone help?
load fisheriris.mat;
x = meas(:,1);
y = meas(:,2);
h = scatterhist(x,y,'Group',species);
hold on;
clr = get(h(1),'colororder');
boxplot(h(2),x,species,'orientation','horizontal',...
'label',{'','',''},'color',clr);
boxplot(h(3),y,species,'orientation','horizontal',...
'label', {'','',''},'color',clr);
set(h(2:3),'XTickLabel','');
view(h(3),[270,90]); % Rotate the Y plot
axis(h(1),'auto'); % Sync axes
hold off;
Thanks in advance.
Cheers,
Fabian

Answers (1)

Adam Danz
Adam Danz on 16 Apr 2021 at 21:20
Edited: Adam Danz on 19 Apr 2021 at 13:31
> I'd like the width of the left boxplot to be the same as the height boxplot below the scatter plot [in scatterhist]
Key points to know
  • In h=scatterhist(__), h is a 1x3 array of axis handles where h(1) is the scatter axes, h(2) is the horizontal axis, and h(3) is the vertical axis.
  • The position of axes is defined by h(n).Position for axis n which returns a 1x4 vector identiying the [x coordinate of lower-left corner, y coordinate of lower-left corner, width height].
  • The position units are defined in h(n).Units which are normalized by default.
  • To equate the width of one axis with the height of another axis, you need to use non-normalized units unless your figure is perfectly square.
Solution
origUnits = get(h(2:3),'Units');
set(h(2:3), 'Units', 'Pixels')
sizeDiff = h(3).Position(3) - h(2).Position(4);
h(3).Position(3) = h(2).Position(4);
h(3).Position(1) = h(3).Position(1)+sizeDiff;
set(h(2:3),{'Units'}, origUnits)
Note that resizing the figure will result in resizing the axes so the width of axis 3 will no longer match the heigh of axis 2 unless the figure resize maintains height:width ratio. You could remove the last line in my solution but that will potentially cause other problems (you can try it and see what I mean).

Community Treasure Hunt

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

Start Hunting!

Translated by