7

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.

Mark C.
  • 1,773
  • 2
  • 16
  • 26

1 Answers1

8

I think you can use the xticks function to set string labels:

nums = arange(2003, 2010)
xticks(nums, (str(n) for n in nums))

EDIT: This is a better way:

gca().xaxis.set_major_formatter(FormatStrFormatter('%d'))

or something like that, anyway. (In older versions of Matplotlib the method was called setMajorFormatter.)

David Z
  • 128,184
  • 27
  • 255
  • 279
  • don't know from which version, but since Matplotlib 1.0 setMajorFormatter is set_major_formatter. – Bernardo Kyotoku Dec 24 '10 at 21:38
  • 2
    Or with full namespaces: matplotlib.pyplot.gca().yaxis.set_major_formatter(matplotlib.ticker.FormatStrFormatter('%d')) – bsa Jan 24 '14 at 02:51
  • 1
    See also http://stackoverflow.com/questions/3677368/matplotlib-format-axis-offset-values-to-whole-numbers-or-specific-number, which suggests using [`ScalarFormatter(useOffset=False)`](http://matplotlib.org/api/ticker_api.html#matplotlib.ticker.ScalarFormatter). –  Sep 15 '14 at 09:12