Why do the x-axis tick marks overlap when using the PLOTYY function in MATLAB?

1 次查看(过去 30 天)
When I execute the commands:
for i=1:0.1:10
P = i*0.9;
Pnew= 1.05*exp(-(3.24*(i*(1.8+i*0.027)-P)/279))*((i-(0.5*exp(3.24*(i*(1.8+i*0.027)-P)/74))));
P=Pnew;
eff=Pnew/(i*1.8);
[ax, h1, h2] = plotyy(i,Pnew,i,eff,'plot');
set(get(ax(1),'Ylabel'),'String','Power')
set(get(ax(2),'Ylabel'),'String','Effeciency')
hold on
end
xlabel('Current[A]')
title('Diode LIV Plot')
I receive a plot with overlapping tick marks along the x-axis.

采纳的回答

MathWorks Support Team
The PLOTYY function in MATLAB behaves this way because of how it handles redrawing on its two axes. The first axes created by PLOTYY is repeatedly used to draw the next plot by setting its "NextPlot" property to "add". However, a new second axes is created every time PLOTYY is called to redraw a plot on the axes.
In this example, the PLOTYY function is repeatedly called from within a FOR loop with different data values. Since these data values have different ranges, the newly-created second axes will have different "Xlim" values. These limits do not match with those of the previously created second axes, hence the x-axis tick marks do not coincide.
To work around this issue, you can explicitly specify the "Xlim" and "Ylim" properties for both axes as shown in the following code:
for i=1:0.1:10
P = i*0.9;
Pnew= 1.05*exp(-(3.24*(i*(1.8+i*0.027)-P)/279))*((i-(0.5*exp(3.24*(i*(1.8+i*0.027)-P)/74))));
P=Pnew;
eff=Pnew/(i*1.8);
[ax, h1, h2] = plotyy(i,Pnew,i,eff,'plot');
set(get(ax(1),'Ylabel'),'String','Power')
set(get(ax(2),'Ylabel'),'String','Efficiency')
axis(ax(1),[1 10 1 10]);
axis(ax(2),[1 10 -1 1.5]);
hold on
end
xlabel('Current[A]')
title('Diode LIV Plot')
This will ensure that the x-axis tick marks remain aligned.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Two y-axis 的更多信息

产品


版本

R14SP2

Community Treasure Hunt

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

Start Hunting!

Translated by