Plot with two different x-axix

32 ビュー (過去 30 日間)
Lodewijk Pleij
Lodewijk Pleij 2018 年 5 月 2 日
コメント済み: Lodewijk Pleij 2018 年 5 月 2 日
Hello,
I have to plot two different values over a certain depth. Those two values have very different ranges, so I would like one of them (k1) to have its x axis at the top, and the other(NESPN1) its x axix on the bottom. I have tried the mathworks page on double x axis, but it won't work. Can you please help me?
if true
figure
plot(k1,Depth1)
ax1.XColor = 'r';
ax1.YColor = 'r';
ax1_pos = ax1.Position; % position of first axes
ax2 = axes('Position',ax1_pos,...
'XAxisLocation','top',...
'YAxisLocation','left',...
'Color','none');
plot(NESNP1,Depth1,'Parent',ax2,'Color','k')
set(gca,'Ydir','reverse')
end
  3 件のコメント
Lodewijk Pleij
Lodewijk Pleij 2018 年 5 月 2 日
thanks, but it will still only plot the NESNP1.
if true
figure
plot(k1,Depth1)
ax1=get(gca)
ax1.XColor = 'r';
ax1.YColor = 'r';
ax1_pos = ax1.Position; % position of first axes
ax2 = axes('Position',ax1_pos,...
'XAxisLocation','top',...
'YAxisLocation','left',...
'Color','none');
plot(NESNP1,Depth1,'Parent',ax2,'Color','k')
set(gca,'Ydir','reverse')
end
Guillaume
Guillaume 2018 年 5 月 2 日
@Jonas, answers go in the answer box, not as comments to the question. This is better for you as you can get any credit for comments.

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

採用された回答

jonas
jonas 2018 年 5 月 2 日
Try this.
clear all;close all
figure;
h=plot(k1,Depth1)
ax1=gca
ax1.XColor = 'r';
ax1.YColor = 'r';
ax1_pos = ax1.Position; % position of first axes
ax2 = axes('Position',ax1_pos,...
'XAxisLocation','top',...
'YAxisLocation','left',...
'Color','none');
plot(NESNP1,Depth1,'Parent',ax2,'Color','k')
set(gca,'Ydir','reverse')
set(ax2,'XAxisLocation','top')
set(ax1,'Ytick',[])

その他の回答 (1 件)

Guillaume
Guillaume 2018 年 5 月 2 日
Other than the issue with the undefined axis that Jonas answered, it seems that you don't know the correct syntax for plot. If I understood correctly, you're plotting against depth. Hence Depth1 should be the first argument to plot not the second. Overall:
figure;
plot(Depth1, k1);
ax1 = gca;
ax2 = axes('Position', ax1.Position, 'XAxisLocation', 'top', 'YAxisLocation', 'left');
plot(Depth1, NESNP1, 'Parent', ax2, 'Color', 'k');
ax2.Color = 'none';
  1 件のコメント
Lodewijk Pleij
Lodewijk Pleij 2018 年 5 月 2 日
Yes this is great! Thanks, the only problem left is that matlab puts both labels on the bottom. Do you know how to put one on the top and the other on the bottom?

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

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by