Matlab : 2 x-axis with one plot
古いコメントを表示
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
採用された回答
その他の回答 (1 件)
Joseph Cheng
2015 年 4 月 22 日
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
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
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
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 についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!