2011-11-17 47 views
6

有沒有人知道如何在matplotlib中將單行的線寬改爲x的函數?例如,如何爲x的較小值創建一條線,並且如何爲較大的x值創建較粗的線?如何將單行的線寬作爲matplotlib中x的函數進行更改?

+0

你能澄清你想要什麼:你在哪裏希望線開始和結束?它只是一條垂直線嗎?此外,你到目前爲止嘗試過什麼,你有什麼問題? – Yann

+0

其基本思想是我有一個x值的數組,以及y值的數組,以及我希望線寬在每個點上的數組。問題在於,當您繪製一條線時,linewidth關鍵字只會佔用一個數字。我找不到任何方法來使它取得一系列的價值。 – cmk24

回答

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