Creating plot with 2 x axes
조회 수: 196 (최근 30일)
이전 댓글 표시
I have written the following snippet of code for creating a plot with two x axes
figure(),
x1 = linspace(0,100,101);
y1 = sin(x1);
plot(x1,y1)
xlabel('Loss (%)')
ylabel('Energy Efficiency (%)')
x2 = x1*5*40000/100;
ax1 = gca;
ax1_pos = ax1.Position;
ax2 = axes('Position', ax1_pos, 'XAxisLocation', 'top', 'YAxisLocation', 'right');
plot(x2, y1, 'Parent', ax2);
I have configured the 2nd x axis to be on the top, but I get the following, where the two x axes overlap:
![untitled1.bmp](https://www.mathworks.com/matlabcentral/answers/uploaded_files/204373/untitled1.bmp)
I tried the code in this post: https://www.mathworks.com/help/matlab/creating_plots/graph-with-multiple-x-axes-and-y-axes.html?searchHighlight=two%20x-axes
and the code works. But I don't understand what's the problem with my code. Could anybody explain please?
댓글 수: 0
채택된 답변
dpb
2019년 2월 16일
If you'll go through step by step, you'll see the problem doesn't occur until the second plot() call -- you didn't hold the second axes and plot by itself resets things when the 'NextPlot' property is set to default which is 'Replace'
Try something like
...
hAx(1)=gca;
hAx(2)=axes('Position',hAx(1).Position,'XAxisLocation','top','YAxisLocation','right','color','none');
hold(hAx(2),'on')
plot(hAx(2),x2,y1)
and see if joy doesn't ensue...
댓글 수: 2
dpb
2019년 2월 21일
편집: dpb
2019년 2월 21일
Because it used line instead of plot to add the data to the axis and line is the low-level routine that just uses the axis as it finds it; plot() is the high-level routine that tries to make things pretty for you.
I probably should have noted the difference rather than just giving the fix; good reminder for future.
The comments on that page should also note the difference explicitly; I'll send TMW support a note on that documentation enhancement.
추가 답변 (2개)
Walter Roberson
2019년 2월 16일
Your ax2 is using the default background color, which is white. It is hiding the other axes.
Mckenzie Dice
2020년 12월 4일
I am having a similar probelm but this code does to work for me. I have four lines to plot: three should be on the bottom x axis and the fourth (avri) should be on the top x axis. This code makes the secondary axis but the avri will not plot to it- it still plots to the wrong axis.
for i=1:295
c=bmus(i);
subplot(4,5,c)
hAx(1)=gca;
plot(av_v(:,c),z_plot, 'r')
hold on
plot(av_u(:,c),z_plot,'b')
hold on
plot(averages(:,c),z_plot,'g')
hold on
xlim([-5,15])
ylabel('height,m')
xlabel('u and v (m/s), t (k)','FontSize',10)
hAx(1)=gca;
hAx(2)=axes('Position',hAx(1).Position,'XAxisLocation','top','YAxisLocation','right','color','none');
hold(hAx(2),'on')
plot(hAx(2),avri(c,:),z_plot,'k')
xlim([-.05,.15])
xlabel('Richardson Number','FontSize',10)
title(c)
legend('v','u','th','avri')
fig = gcf;
fig.Position(3) = fig.Position(3) + 250;
% add legend
legend('Position',[0.15 0.95 0.01 0.02])
legend('boxoff')
end
댓글 수: 4
Walter Roberson
2020년 12월 4일
I do not know? Your for runs to 295, but you pull the subplot number on a 4 x 5 grid out of bmus so bmus must have entries no larger than 20. Your current code puts all of the data for each of the distinct bmus values together, an average of 15 different i values for each subplot, and there is no documentation as to what you really want to do.
hAx(2)=axes('Position',hAx(1).Position,'XAxisLocation','top','YAxisLocation','right','color','none');
Note that this creates a new axes() for each of the 295 i values, so you are averaging overlaying 15 different axes on top of each subplot.
Mckenzie Dice
2020년 12월 4일
So I made the following change
figure(20)
FigH= figure('Position', get(0, 'Screensize'));
c=1;
while c<=20
subplot(4,5,c)
plot(av_v(:,c),z_plot, 'r')
hold on
plot(av_u(:,c),z_plot,'b')
hold on
plot(averages(:,c),z_plot,'g')
hold on
xlim([-5,15])
ylabel('height,m')
xlabel('u and v (m/s), t (k)','FontSize',8)
hAx(1)=gca;
hAx(2)=axes('Position',hAx(1).Position,'XAxisLocation','top','YAxisLocation','right');
hold(hAx(2),'on')
plot(avri(c,:),z_plot,'k')
xlim([-.05,.15])
xlabel('Richardson Number','FontSize',8)
title(c)
% legend('v','u','th','avri')
fig = gcf;
fig.Position(3) = fig.Position(3) + 250;
% add legend
legend('Position',[0.15 0.95 0.01 0.02])
legend('boxoff')
c=c+1;
end
Now I would like to specify to the code that
plot(av_v(:,c),z_plot, 'r')
hold on
plot(av_u(:,c),z_plot,'b')
hold on
plot(averages(:,c),z_plot,'g')
hold on
xlim([-5,15])
should be on one axis (the bottom x axis from -5 to 15)
and plot(avri(c,:),z_plot,'k') should be on the second axis (the top axis from -.05 to .15)
참고 항목
카테고리
Help Center 및 File Exchange에서 Line Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!