Multiple y axes with multiple plots on single x axis

Dear all,
I tried to plot three y-axis where each y-axis contains two lines. I used the approach described in this post: https://nl.mathworks.com/matlabcentral/answers/459385-multiple-y-axes-on-single-x-axis#answer_372987
However, the x-ticks and y-thicks overlap each other. How can I solve this?
close all
figure
grid on
ax1 = axes;
yyaxis left
plot(theta_p{5},dW_closing_winches{5},'linewidth',1.5)
hold on
plot(theta_p{6},dW_closing_winches{6},'linewidth',1.5)
pause(0.1)
xlabel('\theta [%]','FontWeight','bold')
ylabel('Work [J]','FontWeight','bold')
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
plot(theta_p{5},dW_HDP{5},'linewidth',1.5)
hold on
plot(theta_p{6},dW_HDP{6},'linewidth',1.5)
ylabel('Work [J]','FontWeight','bold')
ax2 = axes('position', ax1.Position);
plot(ax2,theta_p{5},(dW_closing_winches{5}+dW_HDP{5}),'k')
hold on
plot(ax2,theta_p{6},(dW_closing_winches{6}+dW_HDP{6}),'--k')
pause(0.1)
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';
dW_HDP{5} = ax2.YLim;
ax2.YTick = linspace(dW_HDP{5}(1), dW_HDP{5}(2), length(ytick)); % see [2]
% horzontally offset y tick labels
ax2.YTickLabel = strcat(ax2.YTickLabel, {' '});

 Risposta accettata

Adam Danz
Adam Danz il 24 Mar 2021
Modificato: Adam Danz il 24 Mar 2021
Take a look at these 3 lines below.
figure
grid on
ax1 = axes;
Line 1 creates a new figure.
grid on not only turns on the grid but also creates axes.
ax1 = axes creates a second set of axes directly on top of the first axes.
To fix this, move grid on to some point after the axes are created and get into the habit of always using parent handles to specify the parent of a graphics object.
fig = figure(); % GET HANDLE
% grid on % REMOVE
ax1 = axes(fig); % SPECIFY PARENT
yyaxis(ax1,'left') % SPECIFY PARENT
plot(ax1,theta_p{5},dW_closing_winches{5},'linewidth',1.5) % SPECIFY PARENT
hold(ax1,'on') % SPECIFY PARENT
grid(ax1,'on') % ADD THIS
you can do the rest :)

9 Commenti

Is it also possible to use in combination with a for loop? So I can plot multiple data sets in one figure. No I have to manually add another plot with the code below.
close all
figure
ax1 = axes;
yyaxis left
p1=plot(theta_p{5},dW_closing_winches{5},'linewidth',1.5);
hold on
p2=plot(theta_p{6},dW_closing_winches{6},'linewidth',1.5);
xlabel('\theta [%]','FontWeight','bold')
ylabel('Work [J]','FontWeight','bold')
pause(0.1)
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
p3=plot(theta_p{5},dW_HDP{5},'linewidth',1.5);
hold on
p4=plot(theta_p{6},dW_HDP{6},'linewidth',1.5);
ylabel('Work [J]','FontWeight','bold')
ax2 = axes('position', ax1.Position);
p5=plot(ax2,theta_p{5},(dW_closing_winches{5}+dW_HDP{5}),'k');
hold on
p6=plot(ax2,theta_p{6},(dW_closing_winches{6}+dW_HDP{6}),'--k');
ylabel('Work [J]','FontWeight','bold')
pause(0.1)
ax2.Color = 'none';
grid(ax2, 'on')
% Horizontally scale the y axis to alight the grid
ax2.XLim = ax1.XLim;
ax2.XTick = ax1.XTick;
ax2.YLimMode = 'manual';
ax2.YTick = linspace(ax2.YLim(1), ax2.YLim(2), length(ytick)); % see [2]
% horzontally offset y tick labels
ax2.YTickLabel = strcat(ax2.YTickLabel, {' '});
set(gcf, 'Position', [500 200 900 760])
title(['Work flat surface - nemaX grab - ',Simulation_names{5},' & ',Simulation_names{6}])
legend([p1;p2;p3;p4;p5;p6],{'Work_{closing winches T50 / 4 s}','Work_{closing winches T50 / 5 s}','Work_{HDP T50 / 4 s}','Work_{HDP T50 / 5 s}','Work_{total T50 / 4 s}','Work_{total T50 / 5 s}'},'location','northwest')
annotation('textbox',[0.12 0.06 0 0],'String','Open','FontWeight','bold');
annotation('textbox',[0.85 0.06 0 0],'String','Closed','FontWeight','bold');
I only looked at your code briefly but it looks like you could do that in a for loop or as you have written it.
I tried a different data set with the same code. However, the x-axis of ax2 is shifted with respect ot ax1. What could be the problem?
It looks like you're not setting the xlim for both axes.
First try using linkaxes (see documentation).
Alternatively, set the xlim of both axes. linkaxes is better because if one axis limit changes, the other will be updated.
With the following lines of code, which are already specified in the code, I assume that the xlim of both axis are set to the same values, right?
% Horizontally scale the y axis to alight the grid
ax2.XLim = ax1.XLim;
ax2.XTick = ax1.XTick;
Adam Danz
Adam Danz il 13 Apr 2021
Modificato: Adam Danz il 13 Apr 2021
That looks OK. Upon closer look, the axis positions appear to be the problem.
Link the axis positions using linkprop.
I figured out what the problem was. I added the following line of code:
set(gcf, 'Position', [500 200 900 760])
But this was only set for the last two plots (p5 and p6). Therefore, those plots didn't align with the previous ones. I added the above line of code to the other plots and it works perfectly.
I want to thank you for your help and I learned a lot from the advised functions.
That's why linkprops is useful. Once the axis positions are linked, changes to one axis will copy to the second axes. Glad I could help.

Accedi per commentare.

Più risposte (0)

Categorie

Prodotti

Release

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by