Matlab : 2 x-axis with one plot
16 views (last 30 days)
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,...
How can I do ? And ho can I choose the scale of the second axis ?
Thank you in advance,
pfb on 22 Apr 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
As to the scale on the second (x) axis, you just need to set the xlim property. E.g.
Or, with focus on ax2 ("axes(ax2)") you can use the xlim function
More Answers (1)
Joseph Cheng on 22 Apr 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;
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'));
% 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');
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.