【问题标题】:Is there a way to mark the min and max of a line chart on its y axis in matplotlib?有没有办法在 matplotlib 的 y 轴上标记折线图的最小值和最大值?
【发布时间】:2020-05-09 06:37:53
【问题描述】:

我正在尝试制作一个我生成的图表,以便比较有用。有没有办法制作如下图所示的东西?或者可能是沿着最大值和最小值的 y 值画一条线的方法?

我尝试过使用 max() 和 min() 并将其放置在这样的图中:

plt.plot(dat[0]['end_date'], max(dat[0]['pct']))

这会引发值错误,因为 x 列表有 48 个条目,而 y 列表只有一个。

ValueError: x and y must have same first dimension, but have shapes (48,) and (1,)

我可以使用这个版本的某个版本,以某种方式用相同的最大值填充剩余的 47 个空格吗?

谢谢!

【问题讨论】:

标签: python python-3.x pandas matplotlib


【解决方案1】:

你可以使用plt.axhline():

plt.axhline(max(dat[0]['pct']))
plt.axhline(min(dat[0]['pct']))

使用随机数据的演示:

import matplotlib.pyplot as plt
import numpy as np, pandas as pd

df = pd.DataFrame({'x':[np.random.randint(0,10) for i in range(10)]})
df.plot()
plt.axhline(df.x.max())
plt.axhline(df.x.min())

结果:

【讨论】:

  • 干杯!祝您编码愉快。
猜你喜欢
  • 2014-10-22
  • 2014-01-03
  • 1970-01-01
  • 2021-07-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多