Multiple y axes with multiple plots on single x axis
조회 수: 12 (최근 30일)
이전 댓글 표시
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?
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/560763/image.png)
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, {' '});
댓글 수: 0
채택된 답변
Adam Danz
2021년 3월 24일
편집: Adam Danz
2021년 3월 24일
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
Adam Danz
2021년 4월 14일
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.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Graphics Object Programming에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!