使用matplotlib修剪尾随xticks零

时间:2014-08-12 21:54:09

标签: python matplotlib

非常使用matplotlib,并且我对xticks有困难。我基本上有一个从0到0.025的x轴。我的问题出现了,因为x轴上大多数精确值的精度似乎设置了它们的精度,例如, 0显示为0.000。我希望它显示为0,因为尾随零是冗余的,类似于其他值。

这是我的...输出在x轴上给出了太多的尾随零:

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')

有没有明显的方法可以做我喜欢的事情,还是我坚持下去?我之前使用过PyX(而且我必须承认我有点混乱,因为我已经学会了使用我的合作者使用过的东西并且他们之间已经变化了)轴是正确的,但似乎并不支持LaTeX,所以它不是一个想法的解决方案。

1 个答案:

答案 0 :(得分:1)

你需要的是这两行:

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

FormatStrFormatter可以接受其他类似sprintf的格式化选项。