Put pi in tick label for 2012

18 vues (au cours des 30 derniers jours)
Josh Heiner
Josh Heiner le 30 Sep 2016
Modifié(e) : dpb le 1 Oct 2016
Hello,
How do I get pi/2 pi and other quantities in the plot x tick labels as the pi symbol (Just so you know I am using 2012 so the help to do this for the 2013+ is different).
In the following I get the tick markers showing up as \pi/4 instead of the pi symbol. They are showing up in the right place.
theta=(0:pi/1000:pi);
plot(theta,sin(theta))
set(gca,'XTick',[0 pi/4 pi/2 3*pi/4 pi],'XTickLabel',{0,'\pi/4','\pi/2','3*\pi/4','\pi'},'YTick',[0 0.5 1])

Réponses (1)

dpb
dpb le 1 Oct 2016
Modifié(e) : dpb le 1 Oct 2016
The TeX interpreter isn't enabled for 'X|Y|ZTick' labels yet in R2012, sorry. You can write them with text if desired.
ADDENDUM With HG2 TMW introduced the axes property 'TickLabelInterpreter'. I'm not sure whether that was with R2013x or R2014x but it was after R2012b...
ADDENDUM 2 :)
It's not that bad to do, actually...the following generates the included version of yours--
>> xt=[0 pi/4 pi/2 3*pi/4 pi].';
>> yt=[0 0.5 1].';
>> plot(theta,sin(theta))
>> xlim([0 pi])
>> set(gca,'XTick',xt,'XTickLabel','', ...
'YTick',yt,'YTickLabel',num2str(yt,'%0.1f'))
>> text(xt,-0.01*ones(size(xt)),{'0','\pi/4','\pi/2','3\pi/4','\pi'}, ...
'horizontal','center','vertical','top')
>>
I added formatted string to y-axis; it's always been one of my pet peeves that the default looks so klunky with different formatting. This is another enhancement with HG2; even though TMW hasn't fixed the default behavior to be less ugly, at least they introduced a 'format' property for the tick labels that lets one specify a format string rather than having to actually rewrite them and just breaking the connection between values. That's a real pit(proverbal)a(ppendage) when use zoom, for example.
As another minor cleanup I changed the x-limits and removed the '*' from the 3pi/4 string; personal preference, salt to suit! :)
The only real difficulty here is that the text y-position is in scaled units such that modifying the y-axis will change its location. One could use 'normalized' units but then have to compute where the x-positions needs must be--a Catch-22 conundrum; to be totally general one needs must compute one or the other; your choice as to which. I generally stick with default and ratio y on a percentage to keep the x values w/ the grid.

Community Treasure Hunt

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

Start Hunting!

Translated by