Multiple y axes on single x axis

492 vues (au cours des 30 derniers jours)
Umang Dongre
Umang Dongre le 30 Avr 2019
Commenté : Rik le 14 Mar 2022
Hello, I want to know how to do three y axis one next to one on left side with space. i'm trying but not getting it properly.
VarName1=A0006_charge(:,1);
VarName2=A0006_charge(:,2);
VarName3=A0006_charge(:,3)/3600;
VarName4=A0006_charge(:,4);
plot(VarName3, VarName1, VarName3, VarName2, VarName3, VarName4);
ylim([-1 5])
Like this.

Réponse acceptée

Adam Danz
Adam Danz le 30 Avr 2019
Modifié(e) : Adam Danz le 26 Mai 2020
Try a function from the file exchange
Or adapt this demo to your needs (yyaxis requires r2016a or later)
Setting up two y axes is trivial. Three y axes, not so trivial. You'll need to set up a plot with and right and left y axes (using yyaxis) and then you'll need to overlay invisible axes on top of the original ones, perfectly placed, and perfectly scaled so the vertical and horizontal ticks align. Finally, add some space to the right of the y tick labels so they are horizontally offset.
Here's a demo that you can adapt to your needs. The critical steps are setting the axis ticks and making sure the spacing is the same between axes. I use the grid feature for both axes to ensure that they are overlayed property (otherwise you'll see double grids).
% Create some data to work with
x = 0:.1:3;
y1 = exp(x);
y2 = log(x);
y3 = x.^2;
% Plot on the left and right y axes
figure
ax1 = axes;
yyaxis left % see [1]
plot(x,y1)
pause(0.1) % see [3]
% set the y(left) and x tick values, make them permanent
% This is the tricky part and shoudl receive a lot of thought when
% you adapt this to your code...
ax1.XTickMode = 'manual';
ax1.YTickMode = 'manual';
ax1.YLim = [min(ax1.YTick), max(ax1.YTick)]; % see [4]
ax1.XLimMode = 'manual';
grid(ax1,'on')
ytick = ax1.YTick;
yyaxis right % see [1]
plot(x,y2)
% create 2nd, transparent axes
ax2 = axes('position', ax1.Position);
plot(ax2,x,y3, 'k')
pause(0.1) % see [3]
ax2.Color = 'none';
grid(ax2, 'on')
% Horizontally scale the y axis to alight the grid (again, be careful!)
ax2.XLim = ax1.XLim;
ax2.XTick = ax1.XTick;
ax2.YLimMode = 'manual';
yl = ax2.YLim;
ax2.YTick = linspace(yl(1), yl(2), length(ytick)); % see [2]
% horzontally offset y tick labels
ax2.YTickLabel = strcat(ax2.YTickLabel, {' '});
% [1] https://www.mathworks.com/help/matlab/ref/yyaxis.html
% [2] this is the critical step to align the grids. It assumes both
% axes contain ticks at the start and end of the y axis
% [3] For some reason when I step through the code, the plots appear
% as they should but when I run the code at it's natural speed
% there are graphics issues. It's as if code execution is
% ahead of the graphics which is annoying. A brief pause
% fixes this (r2019a)
% [4] Scaling is easier if the ticks begin and end at the axis limits
Changes needed to create the double y-axis on the right side instead of the left side
  1. Calculate ytick = ax1.YTick after plotting on the right axis.
  2. After creating ax2, set the y axis location to the right side using ax2.YAxisLocation = 'right';
  3. Pad the left side of the ax2 y-axis ytick lables instead of the right side by changing: ax2.YTickLabel = strcat({' '},ax2.YTickLabel);
For matlab releases prior to 2016a, use plotyy() instead
The code above has been adapted to plotyy().
% Create some data to work with
x = 0:.1:3;
y1 = exp(x);
y2 = log(x);
y3 = x.^2;
% create the first axes with the two y axes
figure
yyh = plotyy(x,y1,x,y2); %see [1]
yyh(1).XTickMode = 'manual';
yyh(1).YTickMode = 'manual';
yyh(1).YLim = [min(yyh(1).YTick), max(yyh(1).YTick)]; % see [4]
yyh(1).XLimMode = 'manual';
grid(yyh(1),'on')
ytick = yyh(1).YTick;
% create 2nd, transparent axes
ax2 = axes('position', yyh(1).Position);
plot(ax2,x,y3, 'k')
pause(0.1) % see [3]
ax2.Color = 'none';
grid(ax2, 'on')
% Horizontally scale the y axis to alight the grid (again, be careful!)
ax2.XLim = yyh(1).XLim;
ax2.XTick = yyh(1).XTick;
ax2.YLimMode = 'manual';
yl = ax2.YLim;
ax2.YTick = linspace(yl(1), yl(2), length(ytick)); % see [2]
% horzontally offset y tick labels
ax2.YTickLabel = strcat(ax2.YTickLabel, {' '});
% [1] https://www.mathworks.com/help/matlab/ref/plotyy.html
% [2] this is the critical step to align the grids. It assumes both
% axes contain ticks at the start and end of the y axis
% [3] For some reason when I step through the code, the plots appear
% as they should but when I run the code at it's natural speed
% there are graphics issues. It's as if code execution is
% ahead of the graphics which is annoying. A brief pause
% fixes this (r2019a)
% [4] Scaling is easier if the ticks begin and end at the axis limits
  15 commentaires
Adam Danz
Adam Danz le 11 Fév 2022
Thanks for the feedback, Sarah.
Rik
Rik le 14 Mar 2022
Comment posted as flag by @Mohsin Tariq:
the simplest answer i have found

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Two y-axis dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by