3

I have written a very simple piece of code using matplotlib where I want to plot integers on both x and y axis. (I am using Python3 on Raspberry pi)

I searched web and found this thread

how to force matplotlib to display only whole numbers on the Y axis

Based on the suggestion, I wrote this code

import matplotlib.pyplot as plt
import matplotlib.ticker as ticker

fig, ax = plt.subplots()
for axis in [ax.xaxis, ax.yaxis]:
    axis.set_major_locator(ticker.MaxNLocator(integer=True))

plt.plot([2013, 2014, 2015, 2016], [20, 30, 40, 50])
plt.ylabel("Hot Dog Sales")
plt.xlabel("year")
plt.show()

But it still produces X axis like this

enter image description here

Community
  • 1
  • 1
Knows Not Much
  • 30,395
  • 60
  • 197
  • 373

1 Answers1

4

Add the following:

plt.ticklabel_format(style='plain',axis='x',useOffset=False)

This forces scientific format off and removes the 2013 offset.

Neapolitan
  • 2,101
  • 9
  • 21