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

时间:2011-11-17 18:25:54

标签: python matplotlib

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

1 个答案:

答案 0 :(得分: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()

enter image description here

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

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()

enter image description here