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

 채택된 답변

pfb
pfb 2015년 4월 22일

0 개 추천

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])

추가 답변 (1개)

Joseph Cheng
Joseph Cheng 2015년 4월 22일

0 개 추천

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

Joseph Cheng
Joseph Cheng 2015년 4월 22일
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
pfb 2015년 4월 22일
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 2015년 4월 22일
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.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

태그

질문:

2015년 4월 22일

댓글:

2015년 4월 22일

Community Treasure Hunt

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

Start Hunting!

Translated by