是否可以在 matplotlib 中绘制具有可变线宽的线?例如:
from pylab import *
x = [1, 2, 3, 4, 5]
y = [1, 2, 2, 0, 0]
width = [.5, 1, 1.5, .75, .75]
plot(x, y, linewidth=width)
这不起作用,因为 linewidth 需要一个标量。
注意: 我知道 *fill_between()* 和 *fill_betweenx()*。因为这些仅填充 x 或 y 方向,所以这些对于您有斜线的情况并不公平。希望填充始终垂直于线。这就是为什么要寻找可变宽度线的原因。
最佳答案
使用 LineCollections。一种按照 this 的方式进行操作的方法Matplotlib 示例是
import numpy as np
from matplotlib.collections import LineCollection
import matplotlib.pyplot as plt
x = np.linspace(0,4*np.pi,10000)
y = np.cos(x)
lwidths=1+x[:-1]
points = np.array([x, y]).T.reshape(-1, 1, 2)
segments = np.concatenate([points[:-1], points[1:]], axis=1)
lc = LineCollection(segments, linewidths=lwidths,color='blue')
fig,a = plt.subplots()
a.add_collection(lc)
a.set_xlim(0,4*np.pi)
a.set_ylim(-1.1,1.1)
fig.show()
关于python - 具有可变线宽的 Matplotlib 图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19390895/