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

17 ビュー (過去 30 日間)
Jørgen Myklebust 2020 年 3 月 2 日
コメント済み: Jørgen Myklebust 2020 年 3 月 3 日
I have to plot a histogram against one y-axis.
bar(hist(data,n))
I also have three datasets that goes against another y-axis.
plot(w,'g-','LineWidth',1.5)
plot(r,'c-','LineWidth',1.5)
plot(g,'m-','LineWidth',1.5)
Then I've got another dataset that has to go against a third y-axis.
plot(x,v,'r--','LineWidth',1.5)
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.

#### 3 件のコメント

Turlough Hughes 2020 年 3 月 2 日
Can you attach the data you're using as inputs?
Which file exchange functions have you tried?
Jørgen Myklebust 2020 年 3 月 2 日
n = 0.5:1:25.5
data = randi(25,8760,1) % too many datapoints to give you the actual data, but this will do.
w = [0.0446413353120292 0.0940964042448439 0.121372980005119 0.131358771864916 0.127988423566389 0.115499376154473 0.0979609361073778 0.0787758371993200 0.0604087827703384 0.0443542257276047 0.0312754612012046 0.0212282588722629 0.0138953796698661 0.00878474160002115 0.00537080233966300 0.00317883503789039 0.00182313469153450 0.00101401464334558 0.000547339013641821 0.000286902211231029 0.000146126026407959 7.23544123378655e-05 3.48460873563609e-05 1.63299702679374e-05 7.44963986142108e-06 3.30953527763503e-06];
r = [0.0271387653958470 0.0770863229493778 0.115174956557659 0.136862370814584 0.141414024816881 0.131514186264852 0.111974517924008 0.0881310671565062 0.0645081520148326 0.0440872670993606 0.0282123172603350 0.0169384308635643 0.00955602357926051 0.00507176148176594 0.00253464357194619 0.00119363191559679 0.000530003194841642 0.000222000728504438 8.77558092805978e-05 3.27485852875761e-05 1.15406844601935e-05 3.84150964680300e-06 1.20808568247170e-06 3.59005267847932e-07 1.00828375876343e-07 2.67674244065171e-08];
g = [0.0394337350138246 0.0602597547368911 0.0835527141261834 0.105115684041137 0.119990879322805 0.124280418632199 0.116796797649382 0.0995939413789165 0.0770563902486737 0.0540951285841574 0.0344573132377427 0.0199149133329043 0.0104435749288667 0.00496928276145731 0.00214541833890487 0.000840434794242614 0.000298723793536455 9.63406038265979e-05 2.81917910936216e-05 7.48530893778544e-06 1.80331064697308e-06 3.94189562378176e-07 7.81832015732127e-08 1.40700466432989e-08 2.29747809864003e-09 3.40393281257451e-10];
% ylim([0 0.15]) I have used this as the ylimit for w, r and g.
v = [0 0 0 0 0.367139430431131 0.502834163918477 0.522153856613164 0.537757579733529 0.537859265581607 0.538833771477196 0.538079549239866 0.537595840971493 0.537111388964062 0.531006943167029 0.522344145919510 0.519543087330099 0.516289824043778 0.508151460804334 0.496569929225097 0.495014645374997 0.486826884751680 0.450351358351976 0.408239186354676 0.365479804502495 0.326346160383228 0.290233999659861 0.259353844346434 0.232486199303644 0.208723582898165 0.187867485004736 0.169700003399278 0.153801486750378 0.139828494011857 0.127498124266926 0.116575923360994 0.106866474443860 0.0982060204856935 0.0904566381414826 0.0835016054049518 0.0772416947652610 0.0715921889340706 0.0664804644706333 0.0618440245624193 0.0576288891768086 0.0537882711751094 0.0502814824886751 0.0470730263394892 0.0441318406497647 0.0414306648924020 0.0389455081792494 0 0 0 0];
% ylim([0 0.593]) I have used this as for v.
P = [0 0 0 0 4 10.7000000000000 19.2000000000000 31.4000000000000 46.8800000000000 66.8700000000000 91.6000000000000 121.810000000000 158 198.600000000000 244 298.500000000000 360 425 493 578 663 710 740 757 768 772 776 779 780 780 780 780 780 780 780 780 780 780 780 780 780 780 780 780 780 780 780 780 780 780 0 0 0 0];
% ylim([0 800]) I have used this for P.
Jørgen Myklebust 2020 年 3 月 2 日
I have tried:
Maybe this one could work?

サインインしてコメントする。

### 採用された回答

Kelly Kearney 2020 年 3 月 3 日
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.

#### 1 件のコメント

Jørgen Myklebust 2020 年 3 月 3 日
Thank you!!! It works and looks great!

サインインしてコメントする。

R2019a

### Community Treasure Hunt

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

Start Hunting!

Translated by