【问题标题】:How can I change plt.plot x axis from 0 to real value?如何将 plt.plot x 轴从 0 更改为实际值?
【发布时间】:2017-01-16 15:31:08
【问题描述】:

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

例如,我想从字典( year_counts= {'2013':7, '2014':20, '2015':10, '2016':37, '2017':1})。但它的轴从 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()

【问题讨论】:

标签: python matplotlib


【解决方案1】:

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

为了摆脱这个偏移,使用

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多