How do I force matplotlib to write out the full form of the x-axis label, avoiding scientific notation?

Mar*_* C. 7 python matplotlib

I've created a simple hexbin plot with matplotlib.pyplot. I haven't changed any default settings. My x-axis information ranges from 2003 to 2009, while the y values range from 15 to 35. Rather than writing out 2003, 2004, etc., matplotlib collapses it into 0, 1, 2, ... + 2.003e+03. Is there a simple way to force matplotlib to write out the full numbers?

Thanks,
Mark C.

Dav*_*d Z 8

我想你可以使用该xticks函数来设置字符串标签:

nums = arange(2003, 2010)
xticks(nums, (str(n) for n in nums))
Run Code Online (Sandbox Code Playgroud)

编辑:这是一个更好的方法:

gca().xaxis.set_major_formatter(FormatStrFormatter('%d'))
Run Code Online (Sandbox Code Playgroud)

或者类似的东西,无论如何.(在旧版本的Matplotlib中调用了该方法setMajorFormatter.)

  • 或者使用完整的命名空间:matplotlib.pyplot.gca().yaxis.set_major_formatter(matplotlib.ticker.FormatStrFormatter('%d')) (2认同)