python - 具有可变线宽的 Matplotlib 图

标签 python matplotlib plot

是否可以在 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()

output

关于python - 具有可变线宽的 Matplotlib 图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19390895/

相关文章:

python - 如何使用 nltk 或 python 删除停用词

python - Seaborn 因子图 : set series order of display in legend

python - 使用字典中的常见值在 python 中绘制图形

python - 从字典列表中动态创建嵌套 json 对象

python - Django REST - 如何获取具有两个模型的 JSON?

python - 使用 python 和 matplotlib 同时获取数据和更新图形

r - 从R中的列表绘制数据

python - 为什么 savefig 和 plot 命令必须位于 IPython 笔记本的同一个单元格中?

python - Django 为什么权限代号与检查是否有权限不同?

python - matplotlib 中距离相关的着色