Why do the x-axis tick marks overlap when using the PLOTYY function in MATLAB?
5 views (last 30 days)
Show older comments
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.
Accepted Answer
MathWorks Support Team
on 26 Jul 2010
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 Comments
More Answers (0)
See Also
Categories
Find more on Two y-axis in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!