Borrar filtros
Borrar filtros

Matlab : 2 x-axis with one plot

3 visualizaciones (últimos 30 días)
Sarah Guiffray
Sarah Guiffray el 22 de Abr. de 2015
Comentada: Joseph Cheng el 22 de Abr. de 2015
Hello, I would like to plot a courb add 2 different x_axis . For the moment, I have only one x_axis and I tried to add an other one but it created also an y_axis but I don't want. My code is :
ax1 = gca;
ax1_pos = ax1.Position;
ax2 = axes('Position',ax1_pos,...
'XAxisLocation','top',...
'Color','none');
How can I do ? And ho can I choose the scale of the second axis ?
Thank you in advance,
Best regards

Respuesta aceptada

pfb
pfb el 22 de Abr. de 2015
I guess that the problem is that the y axis of ax2 overlaps with that of ax1, and the ticks are different. You can remove the ticks of ax2. It should look like there is no additional y axis
set(ax2,'Ytick',[]);
As to the scale on the second (x) axis, you just need to set the xlim property. E.g.
set(ax2,'xlim',[1 2]);
Or, with focus on ax2 ("axes(ax2)") you can use the xlim function
xlim([1 2])

Más respuestas (1)

Joseph Cheng
Joseph Cheng el 22 de Abr. de 2015
well to expand on what pfb says you can try something like this
x1 = 1:50;
x2 = 1:10;
y1 = (1:50)-25;
y2 = rand(1,10)*20;
figure;
plot(x1,y1);
h_ax_line = gca;
% Create a new axes in the same position as the first one, overlaid on top
h_ax_rand = axes('position', get(h_ax_line, 'position'));
plot(x2,y2,'r-');
% set the x limits to the top ', and make the background transparent
set(h_ax_rand, 'XAxisLocation', 'Top', 'xlim', get(h_ax_line, 'xlim'), 'color', 'none');
newlimits = [min([get(h_ax_rand,'yLim') get(h_ax_line,'ylim')]) [max([get(h_ax_rand,'yLim') get(h_ax_line,'ylim')])]];
xlabel(h_ax_line, 'Line x-values','color','b');
xlabel(h_ax_rand, 'random x-values','color','r');
xlim(h_ax_rand,[0 10])
ylim(h_ax_line,newlimits);
ylim(h_ax_rand,newlimits);
in addition you'd need to use get(ax1,'Position') similarly to what i did above to actually get the position. currently i set the Y axes to overlap but with a label you might want to do what pfb did and clear out the 2nd axes.
  3 comentarios
pfb
pfb el 22 de Abr. de 2015
ok, but the question was not how to create new axes on top of the old ones. I think she got that covered.
The question was how to get rid of the second y axis, and set the limits in the second x axis....
Joseph Cheng
Joseph Cheng el 22 de Abr. de 2015
absolutely correct, however as i said i expanded on it with more examples. as well as corrected the position acquiring line. Additionally the information in the example showed the need to set the color of the axes to none possibly preemptively answering another question later on.

Iniciar sesión para comentar.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by