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!

 Akzeptierte Antwort

Benjamin Kraus
Benjamin Kraus am 30 Dez. 2024
Bearbeitet: Benjamin Kraus am 30 Dez. 2024

1 Stimme

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 Kommentare

Benjamin Kraus
Benjamin Kraus am 30 Dez. 2024
Bearbeitet: Benjamin Kraus am 1 Jan. 2025
Note: The order in which you set the properties matters.
Setting either ax.XColor or ax.XAxis.Color will change the value of any color property on the XAxis including the Color, TickLabelColor and x-label Color (Label.Color).
Thanks for the answer!
for some reason this works with ax.XAxis.TickLabelColor = color1 but the next line is ax.YAxis.TickLabelColor = color2 and I get this error:
Assigning to 2 elements using a simple assignment statement is not supported. Consider using comma-separated list assignment.
Error in USV/bpf_btn (line 3271)
ax2.YAxis.TickLabelColor = color2;
^^^^^^^^^^^^^^^^^^^^^^^^
Related documentation
Error using appdesservices.internal.interfaces.model.AbstractModel/executeUserCallback (line 282)
Error while evaluating Button PrivateButtonPushedFcn.
Why does this work with the x axis but not the y axis? in my plot the x axis does change color but not the y. Thanks again
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 am 30 Dez. 2024
Thats exactly what it was, thanks!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Walter Roberson
Walter Roberson am 30 Dez. 2024

0 Stimmen

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 Kommentar

Christian
Christian am 9 Jun. 2025
this works for me... if I add a \
xtickformat('\\color{red}%g')

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Axes Appearance finden Sie in Hilfe-Center und File Exchange

Produkte

Version

R2024b

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by