python - Matplotlib:绘制具有数据坐标中给定宽度的线

标签 python matplotlib

我正在尝试弄清楚如何绘制具有数据单位宽度的线条。例如,在下面的代码片段中,我希望宽度为 80 的线的水平部分始终从 y=-40 延伸到 y=+40 标记,并保持这种状态,即使坐标系的限制改变。有没有办法用 matplotlib 中的 Line2D 对象实现这一点?还有其他方法可以获得类似的效果吗?

from pylab import figure, gca, Line2D

figure()
ax = gca()
ax.set_xlim(-50, 50)
ax.set_ylim(-75, 75)

ax.add_line(Line2D([-50, 0, 50], [-50, 0, 0], linewidth=80))

ax.grid()

line width in screen coordinates

最佳答案

你可以使用 fill_between :

import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.set_xlim(-50, 50)
ax.set_ylim(-75, 75)
x = [-50, 0, 50]
y = np.array([-50, 0, 0])

ax.fill_between(x,y-30,y+30)

ax.grid()
plt.show()

产量

enter image description here

但不同于

生成的行
ax.add_line(Line2D([-50, 0, 50], [-50, 0, 0], linewidth=80))

线条的垂直粗细在数据坐标中始终保持不变。

另见 link to documentation .

关于python - Matplotlib:绘制具有数据坐标中给定宽度的线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14798303/

相关文章:

python - 如何在 OpenCV Python 中识别图像中的不同对象

python - 我想慢慢打印文本并将其作为输入,但最后它说 None

python - 为什么 imshow 的范围参数会翻转我的图像?

python - 如何使用 pylab.imshow() 显示 np.array

python - 在 python 上绘制 (-1,1) 区间内的函数 1/x 和 -1/x^2

python - 如何并行运行多个 Selenium 驱动程序?

python - 在不使用正则表达式的情况下实现 python replace() 函数

python - 从时间序列数据中进行基于相位的事件检测

python - 带有类别的 pyplot 条形图

python - 我通过在列表和数组中附加项目来计算 numpy 数组和普通列表的时间