XLabel and YLabel Font Size

조회 수: 87 (최근 30일)
Thanh Nguyen
Thanh Nguyen 2020년 11월 17일
댓글: Adam Danz 2020년 11월 17일
I'm currently trying to create a script that can automatically format all of my plots for me. I'm encountering a weird issue in trying to set the font size for the XLabel and YLabel.
To demonstrate this problem.
s = tf('s');
H = 1/(s+1); %some function
step(H); %creating a plot
%seeing the properties of XLabel
h = xlabel("Time", "FontSize", 20);
h =
Text (time (seconds)) with properties:
String: 'Time'
FontSize: 20
FontWeight: 'normal'
FontName: 'Helvetica'
Color: [0 0 0]
HorizontalAlignment: 'center'
Position: [223 -20.6667 0]
Units: 'pixels'
So far, everything works and we can print out h and see that it has the correct font size.
However, if we look at the font size property using gca, it doesn't work.
ax = gca;
ax.XLabel
ans =
Text with properties:
String: ''
FontSize: 11
FontWeight: 'normal'
FontName: 'Helvetica'
Color: [0.4000 0.4000 0.4000]
HorizontalAlignment: 'center'
Position: [4.5000 -0.0744 -10]
Units: 'data'
Then, if we use gca to set the XLabel, another label pops up. It seems like there are two separate XLabel properties within the plot.
ax.XLabel.String = "hello";
I tried to replicate this problem with plot(), but that function works fine. Maybe this is unique to step(). I haven't tested with other plotting functions. Any advice on what to do if I want to automate formatting all my plots using gca?
  댓글 수: 3
Thanh Nguyen
Thanh Nguyen 2020년 11월 17일
Right, I did note that they are not the same. The question from me then is how do I acess the XLabel of the plot using gca? Maybe there is another command I'm missing?
Adam Danz
Adam Danz 2020년 11월 17일
"how do I acess the XLabel of the plot using gca? "
Explained here.

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

채택된 답변

Star Strider
Star Strider 2020년 11월 17일
The Control System (and System Identification) Toolbox plot functions properties are difficult to change. That can be done, however it requires serious handle explkorations, and (in my opinion) is generally not worth the effort.
Instead, get the outputs from step and display them using plot:
s = tf('s');
H = 1/(s+1); %some function
[y,t] = step(H); %creating a plot
figure
plot(t, y)
grid
.
  댓글 수: 4
Cris LaPierre
Cris LaPierre 2020년 11월 17일
편집: Cris LaPierre 2020년 11월 17일
Star Strider beat me to it, but I'll add my answer as a comment here, since it includes the outputs.
The title and labels created by the step function are are hidden, so you will not be able to access them without some extra work.
s = tf('s');
H = 1/(s+1); %some function
step(H); %creating a plot
a = findall(gcf,'Type','text')
a =
6×1 Text array: Text (Step Response) Text (Time (seconds)) Text (Amplitude) Text Text Text
a(2)
ans =
Text (Time (seconds)) with properties: String: 'Time (seconds)' FontSize: 11 FontWeight: 'normal' FontName: 'Helvetica' Color: [0 0 0] HorizontalAlignment: 'center' Position: [231.9125 -20.5556 0] Units: 'pixels' Show all properties
My suggestion, then, is if you want to modify the plot, capture the outputs of the step function, then create the plot yourself.
[y,t] = step(H); %creating a plot
plot(t,y)
xlabel("Time", "FontSize", 20);
ylabel("Amplitude","FontSize", 20);
Star Strider
Star Strider 2020년 11월 17일
Cris LaPierre — Thank you!
It was likely only a few seconds difference, however your valuable contribution underscores my original observation about ‘serious handle exploration’. The derivative functions (such as stepplot for step) generally offer more options that are a bit easier to discover. Note that even stepplot required a four-deep handle structure reference to find them and set them.

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

추가 답변 (1개)

Adam Danz
Adam Danz 2020년 11월 17일
편집: Adam Danz 2020년 11월 17일
You can access the axis labels in the step function directly with 2-3 lines of code.
The xlabel and the ax.XLabel return different values which tells you that for whatever reason, the step() function has more than 1 pair of axes. So you need to find the axes handle that contains the displayed xlabel and that's the handle with the "Step Response" title.
Here's how to get the correct axes handle containing the displayed title and change the xlabel fontsize:
% Generate the step plot
s = tf('s');
H = 1/(s+1); %some function
step(H);
% Get the correct axes handle (ax)
axs = findall(gcf,'Type','Axes'); % call immediately after producing figure
titles = axs.Title;
ax = axs(arrayfun(@(t)strcmpi(t.String, 'Step Response'),[axs.Title]));
% Change xlabel fontsize
ax.XLabel.FontSize = 20;

카테고리

Help CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by