How can I change the BackgroundColor on TickLabel

13 Ansichten (letzte 30 Tage)
Tobias Benjamin Gram
Tobias Benjamin Gram am 11 Sep. 2014
Bearbeitet: Star Strider am 12 Sep. 2014
How do I set the backgroundcolor to let say white on the ticklabels and not the axis-label.
Something like this:
set(get(gca,'xticklabel'),'backgroundcolor','w')
But sadly it's not working.

Akzeptierte Antwort

Star Strider
Star Strider am 11 Sep. 2014
Bearbeitet: Star Strider am 12 Sep. 2014
You can do that, but not the way you wrote. You will have to use the text command and its subtleties, for instance Drawing Text in a Box.
You will need to get the 'XTick' values, not only to tell text what to write, but where to put the numbers. (You might also find strsplit helpful.) The section on Text Alignment will help you there. Remember to set 'XTickLabel',[] so they don’t display. Otherwise you will be competing with them. Also, assign ylim to a variable, and set the y-offset to your text call to -0.01 of the diff of that value to start, then adjust as necessary to achieve the result you want.
Here’s an example:
x = linspace(-2*pi,2*pi,250);
y = cos(x*0.25).*sin(10*x);
figure(1)
plot(x, y)
grid
xtk = get(gca,'XTick');
ymin = min(ylim);
yrng = diff(ylim);
set(gca,'XTickLAbel',[])
xlbl = strsplit(num2str(xtk,'%.1f '), ' ');
text(xtk, ymin-0.03*yrng*ones(size(xtk)), xlbl, 'HorizontalAlignment','center', 'VerticalAlignment','top', 'BackgroundColor', 'g')
I coloured the background green here because it otherwise doesn’t show up on the saved .png version:

Weitere Antworten (0)

Kategorien

Mehr zu Labels and Annotations finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by