change color of xticklabels or yticklabels

155 Ansichten (letzte 30 Tage)
Sergio Yanez-Pagans
Sergio Yanez-Pagans am 16 Jul. 2021
Kommentiert: Star Strider am 16 Jul. 2021
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?

Akzeptierte Antwort

Star Strider
Star Strider am 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 Kommentare
Sergio Yanez-Pagans
Sergio Yanez-Pagans am 16 Jul. 2021
Thank you!
Star Strider
Star Strider am 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.
.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by