3

I'm very new to using matplotlib, and I'm having difficulty with the xticks. I basically have an x axis from 0 to 0.025. My problem arises since the precision of the most precise value in the x axis seems to set the precision for them all, so e.g. 0 appears as 0.000. I'd like it to appear as 0 since the trailing zeroes are redundant and analogously for the other values.

Here is what I have... the output gives too many trailing zeroes on the x axis:

from matplotlib import rc
from matplotlib import pyplot
import matplotlib.pyplot as plt

rc('font', **{'family': 'serif', 'serif': ['Computer Modern']})
rc('text', usetex = True)

xmin=0
xmax=0.4
ymin=4.0
ymax=4.5

asq=[0.0217268]
mb=[4.1929] 
mberr=[0.0055]

# some arguments for points etc...

ebargs = dict(mfc='None',alpha=1,ms=8,
      capsize=1.75,elinewidth=0.75,mew=0.75) 

fw = 4.5                    # width
fh = fw/1.618               # height

plt.rc('figure',figsize=(fw,fh))
plt.xlim(xmin,xmax)
plt.ylim(ymin,ymax)

plt.errorbar(x=[x for x in asq],
         y=[y for y in mb],
         yerr=[yerr for yerr in mberr],
         fmt='o',c='b',mec='b', **ebargs
)

plt.savefig("mb-plot.pdf",bbox_inches='tight')

Is there an obvious way to do what I'd like, or am I stuck with it? I used PyX previously (and I must admit I'm getting a bit muddled as I've learned to use each purely through the use of stuff my collaborators have used and they've varied between), which sets the axes properly, but doesn't seem to support LaTeX as well as I'd like, so it's not an idea solution.

Article 82
  • 33
  • 1
  • 4
  • It would be nice (and would get you more answers) if you posted a [minimal, complete code example](http://stackoverflow.com/help/mcve). The problem is not in all the extra module you have there. You can easily remove them and provide a code example that is runnable and details only your problem. – Korem Aug 13 '14 at 05:41
  • Thank you for the suggestion. I've edited the post so that the code is pretty much minimal. Hopefully someone has a solution (or indeed, the bad news that there is no decent solution). – Article 82 Aug 13 '14 at 21:39

1 Answers1

6

What you need are these two lines:

from matplotlib.ticker import FormatStrFormatter
plt.gca().xaxis.set_major_formatter(FormatStrFormatter('%g'))

The FormatStrFormatter can accept other sprintf-like formatting options.

Korem
  • 11,383
  • 7
  • 55
  • 72