MATLAB Answers

Plotting with four y-axes in the same figure, with multiple datasets against one of the y-axes

77 views (last 30 days)
I have to plot a histogram against one y-axis.
I also have three datasets that goes against another y-axis.
Then I've got another dataset that has to go against a third y-axis.
Then atlast I've got a fourth set of data that also needs a seperate y-axis.
plot(x,P,'LineWidth',1.5) % both v and P are plotted against the same x.
In total I therefore need four y-axises. And for atleast one of them I have multiple datasets that has to be plotted.
All, but the first y-axis has to be set like this:
ax = gca;
ax.YColor = 'none';
I have tried to use some functions from the File Exchange, but plotting multiple datasets against one of the extra y-axes has given me a lot of trouble. Any help would be much appreciated.

Sign in to comment.

Accepted Answer

Kelly Kearney
Kelly Kearney on 3 Mar 2020
While there are a lot of FEX options out there, I really think it's easier to manually control things once you start playing around with more than 2 overlapping axes. You just need to be very explicit about which axis each plot command points to. Here's an example:
% Step 1: Create 4 axes, all on top of each other
ax(1) = axes('position', [0.2 0.1 0.7 0.8]);
ax(2) = axes('position', ax(1).Position);
ax(3) = axes('position', ax(1).Position);
ax(4) = axes('position', ax(1).Position);
% Step 2: plot your data on the appropriate axes
bar(ax(1), n, hist(data,n));
hold(ax(2), 'on');
plot(ax(2), 1:length(w), w,'g-','LineWidth',1.5);
plot(ax(2), 1:length(r), r,'c-','LineWidth',1.5);
plot(ax(2), 1:length(g), g,'m-','LineWidth',1.5);
plot(ax(3), x,v,'r--','LineWidth',1.5);
plot(ax(4), x,P,'b', 'LineWidth',1.5);
% Step 3: match up the x-axis limits for all axes. Also, remove axes
% background colors. Make all but one x-axis invisible.
set(ax, 'xlim', [0 26], 'color', 'none');
set(ax(2:end), 'xcolor', 'none', 'ycolor', 'none');
You requested to hide all but the first y-axis, which means you don't need to deal with the overlapping axis lines. But if you do, I like to handle this by 1) moving at least 1 y-axis to the opposite side, and 2) offsetting the remaining axes. I do #2 by creating an additional axis that is linked to the original but displaced, with only the y-axis visible (see offsetaxis.m, here).
set(ax, 'ycolor', 'k'); % ... or just don't set to 'none'
set(ax, 'box', 'off'); % Remove extra y-axis on right side
set(ax(2), 'yaxisloc', 'right'); % Move one y-axis to the right
axo(1) = offsetaxis(ax(3), 'y', 0.1); % offset the others
axo(2) = offsetaxis(ax(4), 'y', 0.2);
set(axo(1), 'ycolor', 'r'); % change the colors of the offset axes to match data
set(axo(2), 'ycolor', 'b');
So this final figure includes 6 different axes, but only certain bits of each are visible, creating the desired look.
Kelly Kearney
Kelly Kearney on 25 Feb 2021
Can you upload some example code that demonstrates what you've tried so far? It looks like you have successfully created at least the 3 overlapping plotting axes; are you getting any error messages when you run offsetaxis?

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!