0

enter image description here

HELLO. I'm now drawing line graph from years and number dictionary data. When I tried to draw line graph by using matplotlib.pyplot, Its axis continuously is fixed 0.

For example, I want to draw graph from dictonary( year_counts= {'2013':7, '2014':20, '2015':10, '2016':37, '2017':1}).But Its axis started with 0 and increased by 1. Even though I set the x limit, plt.xlim(xmin=int(years[0])) OR plt.axis([int(years[0]), int(years[-1]), 0, max(book_counts)]) It doesn't work.

How can I start x-axis with 2013?

This is the code that I used

year_counts=  {'2013':7, '2014':20, '2015':10, '2016':37, '2017':1}

years = sorted(year_counts)
book_counts = [year_counts[year] for year in years]
plt.plot(years, book_counts)
plt.xlim(xmin=int(years[0])) # didn't work
plt.axis([int(years[0]), int(years[-1]), 0, max(book_counts)]) # didn't work
plt.show()
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
dizwe
  • 71
  • 2
  • 10
  • http://matplotlib.org/examples/pylab_examples/spine_placement_demo.html#pylab-examples-example-code-spine-placement-demo-py – wwii Jan 16 '17 at 15:58
  • Possible duplicate of [Center origin in matplotlib](http://stackoverflow.com/questions/4694478/center-origin-in-matplotlib) – wwii Jan 16 '17 at 15:58
  • Possible duplicate of [Matplotlib yaxis range display using absolute values rather than offset values?](http://stackoverflow.com/questions/9303728/matplotlib-yaxis-range-display-using-absolute-values-rather-than-offset-values) – tmdavison Jan 18 '17 at 13:57

1 Answers1

2

Your axis actually starts at 2013, which is 0 + 2.013e3 as seen in the picture.

In order to get rid of this offset, use

ax = plt.gca()
ax.ticklabel_format(useOffset=False)
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712