如何将plt.plot x轴从0更改为实际值?

时间:2017-01-16 15:31:09

标签: python matplotlib

enter image description here

您好。我现在正在绘制年份和数字字典数据的折线图。 当我尝试使用matplotlib.pyplot绘制折线图时,其轴连续固定为0.

例如,我想从dictonary绘制图形( year_counts = {'2013':7,'2014':20,'2015':10,'2016':37,'2017':1} )。但其轴从0开始增加由1。 即使我设置了x限制, plt.xlim(xmin = int(years [0])) OR 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()

1 个答案:

答案 0 :(得分:0)

您的轴实际上从2013年开始,如图所示为0 + 2.013e3

为了摆脱这种偏移,请使用

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