Matlab : 2 x-axis with one plot
Mostrar comentarios más antiguos
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
Más respuestas (1)
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
Joseph Cheng
el 22 de Abr. de 2015
oh and depending on how you want to scale the top x-axis you can either keep what i have in the first set(h_ax_rand,....) where i set the same x-limits to the first axes or remove it to get the axis to scale to the series plotted. (I know i have xlim() at the bottom, it was leftover from a quick implementation test.
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
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.
Categorías
Más información sobre 2-D and 3-D Plots en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!