Creating plot with 2 x axes

323 visualizaciones (últimos 30 días)
Md. Rahman
Md. Rahman el 16 de Feb. de 2019
Comentada: Mckenzie Dice el 4 de Dic. de 2020
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
and the code works. But I don't understand what's the problem with my code. Could anybody explain please?

Respuesta aceptada

dpb
dpb el 16 de Feb. de 2019
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 comentarios
Md. Rahman
Md. Rahman el 20 de Feb. de 2019
Editada: Md. Rahman el 20 de Feb. de 2019
Thank you very much. It works for me. Although I don't understand how this works then:
dpb
dpb el 21 de Feb. de 2019
Editada: dpb el 21 de Feb. de 2019
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.

Iniciar sesión para comentar.

Más respuestas (2)

Walter Roberson
Walter Roberson el 16 de Feb. de 2019
Your ax2 is using the default background color, which is white. It is hiding the other axes.
  1 comentario
Md. Rahman
Md. Rahman el 20 de Feb. de 2019
Thank you very much. Additionally I have to hold axis 2 on, as pointed out by dpb.

Iniciar sesión para comentar.


Mckenzie Dice
Mckenzie Dice el 4 de Dic. de 2020
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 comentarios
Walter Roberson
Walter Roberson el 4 de Dic. de 2020
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
Mckenzie Dice el 4 de Dic. de 2020
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)

Iniciar sesión para comentar.

Categorías

Más información sobre Line Plots en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by