필터 지우기
필터 지우기

Autoarrange title and top xlabel on plot

조회 수: 24 (최근 30일)
Dan H
Dan H 2018년 10월 5일
편집: jonas 2018년 10월 5일
Hello, I have a plot with an extra x-axes on the top (including ticks, numbers, label).
However, the position of this top xlabel is either outside of the plot window (in case of a small window size) or conflicts with title of the plot (bigger window).
Is there a way to "auto-arrange" the position of these text elements, so that independent from window size, title, top xlabel and x-numbers are always visible?
Below you find a simple code example and also a picture, where the "conflict" is even more noticeable.
plot(0,0)
xlabel('time [s]')
primary_axis = gca;
secondary_axis = axes('Position',primary_axis.Position,'XAxisLocation','top','YAxisLocation','right','Color','none'); % secondary_axis = new active axes (top + right)
xlabel('time [h]')
title('example')
Thank you very much, Dan
  댓글 수: 2
Dan H
Dan H 2018년 10월 5일
While jonas' answer worked for the simple example provided with the original question, it does unfortunately not work in the more complex version which I have attached here. The main script is "plot_with_multiple_scales", Jonas' command is included in scaled_axes2.
jonas
jonas 2018년 10월 5일
편집: jonas 2018년 10월 5일
Well, you have to add the title to the axes with an xaxis on top
Add these two lines in your function
title('Title');
primary_axis.Position=secondary_axis.Position
at the location right above this line
axes(primary_axis);
I have adjusted your script, please have a look. As a rule of thumb, output your axes handles if you create them in a loop. Makes it easier to adjust things at the end of the script.

댓글을 달려면 로그인하십시오.

채택된 답변

jonas
jonas 2018년 10월 5일
편집: jonas 2018년 10월 5일
Sure, omit the position argument when you create the secondary axes and then add this at the end of the script
primary_axis.Position=secondary_axis.Position
When you define the position explicitly, then the ActivePositionProperty property of the axes goes from 'outer position' to 'position', and it does not adjust the position when adding title. You want to maintain 'outerposition' until you've added everything, then you can simple align the two axes.
  댓글 수: 1
Dan H
Dan H 2018년 10월 5일
Works with the simple example provided in the original question, but does not yet work with the more complex version (see comment to original question).

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

KALYAN ACHARJYA
KALYAN ACHARJYA 2018년 10월 5일
편집: KALYAN ACHARJYA 2018년 10월 5일
try this one, maybe it helps, this answer is not automated set, but the title will display without overlapping
title({'First line';'';' '})

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by