フィルターのクリア

How to add a second , upper x axis and control the value's locations and text?

147 ビュー (過去 30 日間)
Ron Nativ
Ron Nativ 2020 年 9 月 22 日
コメント済み: Ameer Hamza 2020 年 9 月 22 日
Hi all,
My code generates simple plot and I would like to add a second x-axis with the same scaling as in the first one. However, I cannot seem to control the locations and text where values are placed, from some reason. I would be happy to get your help.
Here is my code:
x = 1:10
y = x.^2;
%
plot(x,y)
xlabel('vector 1')
ylabel('vector 2')
ax1 = gca;
% handle second X-axis
ax2 = axes('Position',get(ax1,'Position'),'XAxisLocation','top','YAxisLocation','right','Color','none','XColor','k','YColor','k');
k=get(ax1,'XTick');
l=get(ax1,'Position');
set(ax2,'YTick',([]));
Thanks!
Ron

採用された回答

Ameer Hamza
Ameer Hamza 2020 年 9 月 22 日
編集済み: Ameer Hamza 2020 年 9 月 22 日
Are you trying to do something like this
x = 1:10;
y = x.^2;
%
ax1 = axes();
box(ax1);
xlabel('vector 1')
ylabel('vector 2')
plot(x,y)
% handle second X-axis
ax2 = axes('Position', get(ax1,'Position'), ...
'XAxisLocation','top', ...
'Color','none', ...
'XColor','k');
ax2.YAxis.Visible = 'off';
ax2.XLim = ax1.XLim;
also add the following line if you want the x-limits to be automatically linked
linkprop([ax1 ax2], 'XLim')
  4 件のコメント
Ron Nativ
Ron Nativ 2020 年 9 月 22 日
編集済み: Ron Nativ 2020 年 9 月 22 日
I found a solution, if anyone is intersted:
%%
axes2 = axes('Parent',figure_name,...
'Position',[0.14 0.15 0.77 0.78]);
xlim(axes2,[xlim1 xlim2]);
% Set the remaining axes properties
set(axes2,'Color','none','XAxisLocation','top','XColor',[0 0 0],...
'XMinorTick','on','XScale','log','XTick',[1 1000 1e6],...
'XTickLabel',{'1 year','1000 years','10^{6} years'});
set(axes2,'YTickLabel',[],'Ytick',[],'Ycolor',[1 1 1])
%%
Ameer Hamza
Ameer Hamza 2020 年 9 月 22 日
I thought that you wanted the same x limits. I am glad that you found the solution. The following code shows an alternate way.
x = 1:10;
y = x.^2;
%
ax1 = axes();
box(ax1);
xlabel('vector 1')
ylabel('vector 2')
plot(x,y)
% handle second X-axis
ax2 = axes('Position', get(ax1,'Position'), ...
'XAxisLocation','top', ...
'Color','none', ...
'XColor','k');
ax2.YAxis.Visible = 'off';
ax2.XLim = [1 1e6];
ax2.XTick = [1 1e3 1e6];
ax2.XTickLabel = {'1 year','1000 years','10^{6} years'};
ax2.XScale = 'log';

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGrid Lines, Tick Values, and Labels についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by