【问题标题】:Draw line on Y-axis as shown in attached figure, on existing X-Y plot in matplotlib在 matplotlib 中现有的 X-Y 图上绘制 Y 轴上的线,如图所示
【发布时间】:2019-11-04 17:04:51
【问题描述】:

我有使用 matplotlib 绘制的 X-Y 数据,我想在已知 X 坐标的 Y 轴上画一条线,类似于附图。最好(最聪明)的方法是什么?

test_plot

我使用 matplotlib 的代码如下:

import matplotlib.pyplot as plt
year = [1960, 1970, 1980, 1990, 2000, 2010]
sample = [45.91, 20.09, 98.07, 107.7, 158.5, 70.6]
plt.plot(year, sample, color='red')
plt.xlabel('Year')
plt.ylabel('Sample')
plt.title('Sample graph')
plt.show()

【问题讨论】:

  • 要确定执行此操作的最佳方法,我们需要查看您已经编写的代码。
  • 我只是使用 X,Y 绘制,还没有绘制一条线。我不知道该怎么做,所以我发布了一些建议。
  • 所以你只想要一条垂直线?
  • 是的,与现有的 X-Y 图相交。imgur.com/a/9d7mKAb

标签: python matplotlib


【解决方案1】:

我相信您正在寻找的是 matplotlib 图的 axvline 或 axhline 方法。

文档:https://matplotlib.org/3.1.0/api/_as_gen/matplotlib.pyplot.axvline.html

plt.axvline(x_value, ymin=0, ymax=1)

如果需要,使用ymax 使行的顶部适合您的数据(标准化在 0 和 1 之间)。类似ymax=lineMax/plt.gca().get_ylim()[1]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-24
    • 1970-01-01
    • 2022-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-13
    • 2020-01-15
    相关资源
    最近更新 更多