how to change axis font color?

130 visualizzazioni (ultimi 30 giorni)
Bradley
Bradley il 30 Dic 2024
Commentato: Christian il 9 Giu 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!

Risposta accettata

Benjamin Kraus
Benjamin Kraus il 30 Dic 2024
Modificato: Benjamin Kraus il 30 Dic 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 Commenti
Walter Roberson
Walter Roberson il 30 Dic 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 il 30 Dic 2024
Thats exactly what it was, thanks!

Accedi per commentare.

Più risposte (1)

Walter Roberson
Walter Roberson il 30 Dic 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 Commento
Christian
Christian il 9 Giu 2025
this works for me... if I add a \
xtickformat('\\color{red}%g')

Accedi per commentare.

Categorie

Scopri di più su Graphics in Help Center e File Exchange

Tag

Prodotti


Release

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by