How to correctly include top x axis label when using two x axis?
조회 수: 46 (최근 30일)
이전 댓글 표시
I want to make a plot with two x-axis, one on the bottom as usual and the other one on the top.
I followed this steps: https://www.mathworks.com/help/matlab/creating_plots/graph-with-multiple-x-axes-and-y-axes.html
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')
댓글 수: 0
답변 (1개)
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])
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Visual Exploration에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!