## SAGE tip: Changing the ticks of a plot

January 25, 2011 by doxdrum

This trick can be found at the plot function documentation, and it was a new command from SAGE(math) v.4.5 or v.4.6.

Say we want to plot the sine function,

`plot(sin, -7, 7, axes_labels=['$x$','$\\sin(x)$'], fontsize=14, color='red')`

we’ll get

Original sine ticks

Now, let’s change the ticks,

`plot(sin(x), (x, -7, 7), ticks=pi/2, tick_formatter=pi, axes_labels=['$x$','$\\sin(x)$'], fontsize=14, color='red')`

We’ll get,

Ticks every half pi.

One can also insert ticks at will!!!

`plot(2*x+1,(x,0,5),ticks=[[0,1,e,pi,sqrt(20)],2],tick_formatter="latex")`

yields,

Ticks at will

Enjoy!

DOX

### Like this:

Like Loading...

*Related*

on January 26, 2011 at 11:50 am |JIs there are way to position a tick at, say, x=pi, but label it in latex as “x_0”? This is commonly needed if you want to make a more general or generic graph of a function for a publication or for teaching.

on January 27, 2011 at 11:52 am |kcrismanAnswer is yes, but you’d have to know a little more about how to make Matplotlib tick formatters and use that. Mpl does location and formatting separately. But presumably one could write a formatter which would do this – see http://matplotlib.sourceforge.net/api/ticker_api.html#matplotlib.ticker.FuncFormatter