http://matplotlib.sourceforge.net/api/ticker_api.html#matplotlib.ticker.FuncFormatter

2010/4/10 konstellationen <konstellationen@...287...>:

For future reference, the solution proposed by Gökhan and Diakronik is to

replace the Latex tick-labels with strings:

import matplotlib.pyplt as plt

tick_locs = range(start, stop, increment)

plt.xticks(tick_locs, [r"$\mathbf{%s}$" % x for x in tick_locs])

If you have twin x or y axes (my case), the solution I found was:

(Note: this solution is essentially the same as the one above, with the

distinction that every entry is set manually, which allows for more

flexibility, but requires more work)

from mpl_toolkits.axes_grid.parasite_axes import SubplotHost

from matplotlib.pylab import * # For plotting graphs.

from matplotlib.pyplot import *

fig=figure(1)

host= SubplotHost(fig,111)

fig.add_subplot(host)

par=host.twiny()

host.axis["bottom"]

par.axis["top"]

hostv=[1e-14,1e-4,-1.5,1.5]

host.axis(hostv)

parv=[1e-8,1e2,-1.5,0.5]

par.axis(parv)

host.set_xticks([1e-14, ... ,1e-4])

x_labels = [r'\boldmath $10^{-14} $', ... ,r'\boldmath $ $']

host.set_xticklabels(x_labels)

par.set_xticks([1e-8, ... ,1e2])

parx_labels = [ r'\boldmath $10^{-8}$', ... ,r'\boldmath $ $' ]

par.set_xticklabels(parx_labels)

host.set_yticks([-1,0])

y_labels = [r'\boldmath $-1$', r'\boldmath $0$']

host.set_yticklabels(y_labels)

Result:

http://old.nabble.com/file/p28199345/Picture%2B7.png

There is another technique based on the FuncFormatter or the

FormatStrFormatter in matplotlib.ticker, see the link at the very top.

It makes less efford when one can rely on the automatic ticking

mechanism and when one has access to the axis (with i) instances. It

is:

To obtain math-formatted number output:

formatter = matplotlib.ticker.FormatStrFormatter('$%g$')

axes.xaxis.set_major_formatter(formatter)

The most important is that one has no longer to set the tick locations manually.

For exponential ticks, I would propose (but it's untested):

def exp_fmt(loc):

exponent = numpy.round(numpy.log10(loc))

return '$10^%d$' % exponent

formatter = matplotlib.ticker.FuncFormatter(exp_fmt)

# And so on.

Note that using r'$\mathbf{%g}$' makes, for me, no difference. It may

be that one needs matplotlib.rc('text', usetex = True) to make also

numbers bold by \mathbf{}, but iirc, also in LaTeX numbers are always

plain, also in \mathbf{}. \boldmath$$ may be an exception from this

rule.

fwiw,

Friedrich

P.S.: I cannot test usetex = True at the moment, because I end up with

the error 'Could not obtain dvipng version'.