how to change axis font color?
58 views (last 30 days)
Show older comments
Bradley
on 30 Dec 2024 at 19:20
Edited: Benjamin Kraus
on 1 Jan 2025 at 12:59
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!
0 Comments
Accepted Answer
Benjamin Kraus
on 30 Dec 2024 at 19:29
Edited: Benjamin Kraus
on 30 Dec 2024 at 19:39
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 Comments
Walter Roberson
on 30 Dec 2024 at 22:06
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;
More Answers (1)
Walter Roberson
on 30 Dec 2024 at 22:42
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
0 Comments
See Also
Categories
Find more on Interactive Control and Callbacks in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!