如何在matplotlib中将单行的线宽更改为x的函数?

cmk*_*k24 6 python matplotlib

有没有人知道如何在matplotlib中将单行的线宽变化为x的函数?例如,对于x的小值,如何使线条变薄,对于较大的x值,如何使线条变粗?

Joe*_*ton 8

基本上,您需要使用多边形而不是线.作为一个简单的例子:

import numpy as np
import matplotlib.pyplot as plt

# Make the original line...
x = np.linspace(0, 10, 100)
y = 2 * x
thickness = 0.5 * np.abs(np.sin(x) * np.cos(x))

plt.fill_between(x, y - thickness, y + thickness, color='blue')
plt.show()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

或者,如果您想要更贴近您的描述:

import numpy as np
import matplotlib.pyplot as plt

# Make the original line...
x = np.linspace(0, 10, 100)
y = np.cos(x)
thickness = 0.01 * x

plt.fill_between(x, y - thickness, y + thickness, color='blue')
plt.show()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述