How to add a second x-axis on top in plot?

82 ビュー (過去 30 日間)
Nadatimuj
Nadatimuj 2021 年 7 月 12 日
編集済み: John R 2021 年 12 月 8 日
I think something similar is asked in the forum before and there is a documentation on how to add second a y-axis. But I am not exactly getting what I want. It would be great if someone can help.
I want something very simple. I have x1, x2, and corresponding y. I want to plot y and show x2 at the bottom axis and x1 at the top axis. Please remember, x1 and x2 are related, so x1 = 14 will refer to x2 = 399 and they will plot y =1..and so on.
x1 = [14 16 18 20 22];
x2 = [399 520 675 830 1001];
y =[1 2 3 4 5];
plot(x2,y)

回答 (2 件)

Jonas
Jonas 2021 年 7 月 12 日
this may be the function you are searching for
  1 件のコメント
Nadatimuj
Nadatimuj 2021 年 7 月 12 日
Thanks but no, not this one. This one requires y1 and y2 that means two seperate plots and the x axes are not related.... But I want only one y plot and two related x-axes for the same plot.

サインインしてコメントする。


John R
John R 2021 年 11 月 5 日
編集済み: John R 2021 年 12 月 8 日
x1 = [14 16 18 20 22];
x2 = [399 520 675 830 1001];
y =[1 2 3 4 5];
t=tiledlayout(1,1);
ax1=axes(t);
set(ax1,'XLim',[min(x1) max(x1)]);
plot(ax1,x1,y)
ax1.XTick=x1;
ax1.YTick=[];
ax1.XTickLabel=x1;
ax2=axes(t);
set(ax2,'XLim',[min(x1) max(x1)]);
plot(ax2,x1,y)
ax2.XAxisLocation='top';
ax2.XTick=x1;
ax2.XTickLabel=x2;

カテゴリ

Help Center および File ExchangeAnnotations についてさらに検索

タグ

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by