如何将 plt.plot x 轴从 0 更改为实际值? [英] How can I change plt.plot x axis from 0 to real value?

查看:76
本文介绍了如何将 plt.plot x 轴从 0 更改为实际值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

例如,我想从字典中绘制图形( 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)])它不起作用.

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.

如何从 2013 开始 x 轴?

How can I start x-axis with 2013?

这是我使用的代码

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

推荐答案

你的坐标轴实际上是从 2013 年开始的,也就是图中的 0 + 2.013e3.

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)

这篇关于如何将 plt.plot x 轴从 0 更改为实际值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆