why does set(gca) not working

66 ビュー (過去 30 日間)
Zeyuan
Zeyuan 2025 年 9 月 27 日 20:41
コメント済み: Paul 2025 年 9 月 28 日 19:05
I am trying to set the axis label's font size. Here is my code
%% This section of code plots the k-means cost as a function of the number
% of iterations
% Count the number of iterations.
figure;
x = 1:max_iter; % this is the x axis
grid on;
set(gca,'fontsize',FONT_SIZE) % THIS IS WHEN I TRIED TO USE GCA BUT IT DOES NOT WORK
plot(x,cost_iteration,'bo-'); % plot the cost per iteration
title("Graph of K-means Cost Per Iteration", 'Fontsize',FONT_SIZE);
xlabel("iteration", 'Fontsize',FONT_SIZE);
ylabel("cost", 'Fontsize',FONT_SIZE);
axis normal;
I tried to use set to make the font size of gca work, but it does not work. and here is what I print out.
please help

採用された回答

Torsten
Torsten 2025 年 9 月 27 日 20:53
移動済み: Torsten 2025 年 9 月 27 日 21:26
The set command must be placed after the plot command, not before:
FONT_SIZE = 16;
max_iter = 10;
x = 1:max_iter; % this is the x axis
cost_iteration = x;
figure
plot(x,cost_iteration,'bo-'); % plot the cost per iteration
set(gca,'fontsize',FONT_SIZE) % THIS IS WHEN I TRIED TO USE GCA BUT IT DOES NOT WORK
grid on;
title("Graph of K-means Cost Per Iteration");
xlabel("iteration");
ylabel("cost");
axis normal;
  4 件のコメント
Stephen23
Stephen23 2025 年 9 月 28 日 2:02
編集済み: Stephen23 2025 年 9 月 28 日 2:22
@Paul this is a property of the AXES, not of the high-level plotting commands like PLOT, thus information on this behavior is found in the overviews about how AXES work, e.g.:
which states: "By default, new plots clear existing plots and reset axes properties, such as the title."
as well as in general plotting advice:
which goes into a lot more detail. The documentation for HOLD also gives a bit of info on this.
I do not know of an official list of the low-level vs high-level plotting functions. Previously this existed:
which stated "The low-level graphics functions are line, patch, rectangle, surface, text, image, axes, and light". These functions do not reset any AXES properties.
Paul
Paul 2025 年 9 月 28 日 19:05
Considering that we're talking about the behavior of plot, which is where I'd imagine most users would start for determining what's going on here, it would be a good idea if that doc page would, at least, have a link to that "preparing ...." page in the Tips section. Something like, "See preparing-figures-... for explanation of how plot can impact figure and axes properties."

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

その他の回答 (1 件)

Image Analyst
Image Analyst 2025 年 9 月 28 日 15:29
I know you already accepted an answer but the more modern alternative (2022a and later) is to use fontsize:
hFig = figure;
cost_iteration = rand(1, 30);
x = 1:numel(cost_iteration); % This is the x axis
plot(x,cost_iteration,'bo-'); % Plot the cost per iteration
grid on;
title("Graph of K-means Cost Per Iteration");
fontsize(20, 'points'); % Affects titles and labels
xlabel("iteration");
ylabel("cost");
axis normal;
Note that it will apply that font size to all axes labels, tick labels, titles, etc. regardless of where it is placed in your code, as long as it's after the call to plot(). Note I put it after title and before xlabel yet it applies to both of them. Then you don't have to use the 'fontsize' option for each individual function like you did, unless you want each label to have it's own unique size.
  1 件のコメント
Paul
Paul 2025 年 9 月 28 日 18:58
I think the OP only used those individual functions because the set(gca, ...) prior to the plot command didn't yield the expected behavior.
Using set(gca,'FontSize', ...) after the call to plot (or prior followed by hold) should control "The font size affects the title, axis labels, and tick labels." (among other things) according to Axes Properties, though that same page goes on to say that the titles and lables are actually 110% of the FontSize by default.

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

カテゴリ

Help Center および File ExchangeGraphics Performance についてさらに検索

タグ

製品


リリース

R2025b

Community Treasure Hunt

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

Start Hunting!

Translated by