2017-01-16 780 views
0

enter image description here如何將plt.plot x軸從0更改爲實際值?

你好。我現在正在繪製年線和數字字典數據的線形圖。 當我試圖通過使用matplotlib.pyplot繪製線圖,它的軸線連續地固定0

例如,我想從dictonary( year_counts = {「2013」​​繪製曲線圖:7,「2014」 :20,'2015':10,'2016':37,'2017':1}).But其座標軸從0開始並增加1. 儘管我設置了x極限, plt.xlim(xmin = int(years [0])) plt.axis([int(years [0]),int(years [-1]),0,max(book_counts)]) 它不起作用。

如何在2013年啓動x軸?

這是我用

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() 
+0

http://matplotlib.org/examples/pylab_examples/spine_placement_demo.html#pylab-examples-example-code-spine-placement-demo- py – wwii

+0

[Matplotlib中的中心原點]可能的重複(http://stackoverflow.com/questions/4694478/center-origin-in-matplotlib) – wwii

+0

好的我會檢查它 – dizwe

回答

0

實際開始您的軸線在2013,這是0 + 2.013e3如圖片可見的代碼。

爲了擺脫這種偏移,使用

ax = plt.gca() 
ax.ticklabel_format(useOffset=False) 
+0

謝謝我解決了它! – dizwe