change color of xticklabels or yticklabels

196 views (last 30 days)
I need help with this:
ax = gca;
ax.XColor = 'w';
ax.YColor = 'w';
changes the color of the axis and the xticklabels. I want the x axis to be a certain color, but the xticklabels another color.
Any suggestions?

Accepted Answer

Star Strider
Star Strider on 16 Jul 2021
One approach —
x = 0:0.1:10;
y = exp(-(x-5).^2);
figure
plot(x, y)
hold on
plot(xlim, [0 0]+min(ylim), 'g')
hold off
Ax = gca;
xt = Ax.XTick;
xtv = compose('%.0f',xt);
Ax.XAxis.Visible = 'off';
text(xt,zeros(size(xt)), xtv, 'Color','r', 'Horiz','center', 'Vert','top')
This turns the x-axis visibility off, draws the x-axis in green, and plots the tick values in red. The ticks themselves are also now invisible, they would have to be added back with a plot call if desired.
.
  2 Comments
Star Strider
Star Strider on 16 Jul 2021
As always, my pleasure!
Also, for the y-tick labels, the text call would be:
text(zeros(size(yt)), yt, ytv, 'Color','r', 'Horiz','right', 'Vert','middle')
although that could require some experimentation.
.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!