On Friday, June 1, 2012 4:48:51 PM UTC+1, kcrisman wrote:
>
>
>
> On Friday, June 1, 2012 11:22:13 AM UTC-4, ObsessiveMathsFreak wrote:
>>
>> While the "ticks=" option in plot can be used to place custom tick marks
>> on plot axes, how would you go about putting custom labels on each tick
>> mark, preferably latex ones,. e.g. $x_0$, $x_1$, etc.
>>
>> Is there a way of doing this automatically, given a list of ticks and
>> labels?
>>
>>
> Yep, but you'd probably have to use a tiny bit of matplotlib. You may
> find the FuncFormatter [1] useful. Go to the Graphics object documentation
> [2] to see exactly how to then use such a formatter.
>
> [1]
> http://matplotlib.sourceforge.net/api/ticker_api.html#matplotlib.ticker.FuncFormatter
> [2] http://sagemath.org/doc/reference/sage/plot/graphics.html
>
> - kcrisman
>
I've read a bit of the documentation, and I've managed to come up with the
following code which half works
import matplotlib.ticker
#function to make life easier
def latex_ticklabels(lbls_x,lbls_y):
#need to prefix empty string to labels for some reason(Don't know
myself).
return [matplotlib.ticker.IndexFormatter(["$\
$"]+lbls_x),matplotlib.ticker.IndexFormatter(["$\ $"]+lbls_y)]
#the labels we are going to use (Must match number of ticks I think)
x_labels=["$x_1$","$x_2$"]
y_labels=["$y_1$","$y_2$"]
P0=plot(x^2,(r,0,2),ticks=[[1,2],[0.5,2.4]],tick_formatter=latex_ticklabels(x_labels,y_labels),fontsize=15)
show(P0)
The problem is that if the tick marks are too close together, the labels
repeat themselves, or seem not to appear. It's very frustrating. For
example try
P0=plot(x^2,(r,0,2),ticks=[[1,2],[0.5,1]],tick_formatter=latex_ticklabels(x_labels,y_labels),fontsize=15)
show(P0)
Anyway, hopefully this will work for now.
--
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/sage-support
URL: http://www.sagemath.org