【问题标题】:Add a date event on a line chart in Python在 Python 中的折线图上添加日期事件
【发布时间】:2020-07-22 17:59:35
【问题描述】:

所以,我有一个折线图,显示 2010 年到 2020 年的随机销售数据。但是,我想添加一条垂直线或一些视觉资源来指示 2014 年发生的重要事情。我怎样才能在 Python 中做到这一点?任何图书馆都可以!

【问题讨论】:

标签: python pandas numpy matplotlib charts


【解决方案1】:

尝试将 plt.axvline() 与 matplotlib 一起使用

import matplotlib.pyplot as plt

x = [ 2015, 2016, 2017, 2018,2019,2020]
y = [ 1000, 1200, 2500, 1000, 1100,250]

plt.plot(x,y)

plt.title("Sales Bar graph")
plt.xlabel("year")
plt.ylabel('Sales')

#drwa a line in 2019 value
plt.axvline(x=2019, label='line at x = {}'.format(2019), c='red')
plt.show()

【讨论】:

  • 这很完美,但是,有没有办法可以给它添加一个图例?
  • 确定你可以在axvline代码之后添加这个:plt.legend()
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多