Matplotlib:绘制宽度在数据坐标中给出的线条

mvo*_*ske 6 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()
Run Code Online (Sandbox Code Playgroud)

屏幕坐标中的线宽

unu*_*tbu 7

你可以使用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()
Run Code Online (Sandbox Code Playgroud)

产量

在此输入图像描述

但不像生成的线

ax.add_line(Line2D([-50, 0, 50], [-50, 0, 0], linewidth=80))
Run Code Online (Sandbox Code Playgroud)

线的垂直厚度在数据坐标中始终是恒定的.

另请参阅文档链接.

  • 线的粗细不是恒定的......它的高度是.这些对于水平线是相同的,但非常陡峭的部分将更加瘦. (2认同)