why does set(gca) not working
Mostrar comentarios más antiguos
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
Respuesta aceptada
Más respuestas (1)
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 comentario
Paul
el 28 de Sept. de 2025
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.
Categorías
Más información sobre Graphics Performance en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


