Creating plot with 2 x axes
이전 댓글 표시
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:

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?
채택된 답변
추가 답변 (2개)
Walter Roberson
2019년 2월 16일
1 개 추천
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일
You have hold on but no hold off, so you are plotting 295 times 4 lines in a single plot, but expect that a legend() with 4 items will be enough.
You do not do any drawnow() or pause() or figure(), so you are not animating -- nothing would be drawn on the display until the end of the for i loop.
Mckenzie Dice
2020년 12월 4일
So where would I put the hold off? just after plotting
plot(hAx(2),avri(c,:),z_plot,'k')
?
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)
카테고리
도움말 센터 및 File Exchange에서 Graphics Object Properties에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!