フィルターのクリア

How to correctly include top x axis label when using two x axis?

46 ビュー (過去 30 日間)
Lucas Pavlov
Lucas Pavlov 2020 年 11 月 7 日
回答済み: Shahmeer Baweja 2021 年 12 月 8 日
I want to make a plot with two x-axis, one on the bottom as usual and the other one on the top.
However, there aren't any x and y labels in that example.
In particular, I want to put x labels both in the top and bottom x axis. I'm able to do that. However, the top x label is somewhat "hidden", i.e. it is not showing properly. Here's an example of how it looks:
And when I save the figure as png, I get this:
Clearly, the top x label is displaying incorrectly. I'm able to see it properly if I stretch the figure in the vertical direction; however, I need an automatic solution so I can save multiple figures without having to manually resize them (and, in addition, I think it should be possible to have the top x axis label correcly displayed regardless of the dimensions of the figure itself).
Here's the code that I used to create this example:
x1 = 0:0.1:40;
y1 = 4.*cos(x1)./(x1+2);
figure
line(x1,y1,'Color','k')
ax1 = gca; % current axes
ax1_pos = ax1.Position; % position of first axes
ax2 = axes('Position',ax1_pos,...
'XAxisLocation','top',...
'YAxisLocation','left', ...
'Color','none') ;
ax2.XLim = ax1.XLim ;
ax2.YLim = ax1.YLim ;
xlabel(ax1, 'x1')
xlabel(ax2, 'x2')

回答 (1 件)

Shahmeer Baweja
Shahmeer Baweja 2021 年 12 月 8 日
I just figured!!
Use InnerPosition: Inner size and location, specified as a four-element vector of the form [left bottom width height]. This property is equivalent to the Position property which resize the red box in the Figure below:
Decrease the value of the 'height' for both axes (ax1 and ax2). I decreased it from 0.8150 to 0.78 so that the top x axis label is also pushed down
set(ax1,'InnerPosition',[0.1300 0.1100 0.7750 0.78])
set(ax2,'InnerPosition',[0.1300 0.1100 0.7750 0.78])

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by