how to change axis font color?

111 vues (au cours des 30 derniers jours)
Bradley
Bradley le 30 Déc 2024
Commenté : Christian le 9 Juin 2025
Im making an app and I want to allow users to change the color of all font on the plot, I have a drop down menu with various color options and when I select one then click 'plot', I want the color to change. I tried set(gca, 'Color', color) but that changes the color of the background, which is good to know but not exactly what I want. I figured FontColor would work but I guess thats not a recognized property. Any ideas on how to update color of the text given user input? Thanks!

Réponse acceptée

Benjamin Kraus
Benjamin Kraus le 30 Déc 2024
Modifié(e) : Benjamin Kraus le 30 Déc 2024
There are several color related properties on the axes, but the one you are looking for is the XColor or YColor (or ZColor).
ax = axes;
xlabel("X Label");
ylabel("Y Label");
set(ax,'LineWidth',4) % So the colors are more obvious
set(ax,'XColor','green');
set(ax,'YColor','red');
You can see the full list of properties on the Axes Properties documentation page, but here are a few more properties that will give you more fine-grained control:
figure
ax = axes;
view(3) % So you can see the z-axis
xlabel(ax,'X Label');
ylabel(ax,'Y Label');
zlabel(ax,'Z Label');
grid(ax,'on');
ax.XMinorGrid = true;
ax.LineWidth = 4; % To make the colors more prominent
ax.XColor = [1 0 0];
ax.YColor = [0 1 0];
ax.ZColor = [0 0 1];
ax.GridColor = [1 1 0];
ax.MinorGridColor = [1 0 1];
ax.XAxis.Color = [1 0 0]; % Redundant with ax.XColor
ax.XAxis.TickLabelColor = [0 1 1];
ax.XAxis.Label.Color = [0 1 0];
  4 commentaires
Walter Roberson
Walter Roberson le 30 Déc 2024
Do you happen to have yyaxis in effect? If so then you probably need one of
ax2.YAxis(1).TickLabelColor = color2;
or
ax2.YAxis(2).TickLabelColor = color2;
Bradley
Bradley le 30 Déc 2024
Thats exactly what it was, thanks!

Connectez-vous pour commenter.

Plus de réponses (1)

Walter Roberson
Walter Roberson le 30 Déc 2024
To change the font color while leaving alone the grid lines, see these examples
xtickformat('\color{red}%g') %red
ytickformat('\color[rgb]{0.8,0.3,0.6}%g') %some random color
title('appropriate title', 'Color', 'b') %blue
xlabel('appropriate label', 'Color', [0.4 0.6 0.7]) %some random color
The xtickformat and ytickformat use a different syntax than title() and xlabel() -- you are adding 'tex' commands into the tick labels. You can do this because the default TickLabelInterpreter property is 'tex' .
If you choose to instead use LaTex specification, you will need to add
ax = gca;
ax.XAxis.TickLabelInterpreter = 'latex';
ax.YAxis(1).TickLabelIntepreter = 'latex';
if length(ax.YAxis) > 1; ax.YAxis(2).TickLabelIntepreter = 'latex'; end
  1 commentaire
Christian
Christian le 9 Juin 2025
this works for me... if I add a \
xtickformat('\\color{red}%g')

Connectez-vous pour commenter.

Catégories

En savoir plus sur Graphics dans Help Center et File Exchange

Tags

Produits


Version

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by