2013-11-21 170 views
3

我想要一條曲線的曲線說x^5 + x^4 + x^3 + x + 1每個點x都取自一個正態分佈。 我有一個向量的手段和西格馬值向量。繪製一個概率密度

使用matplotlib.pyplot我可以繪製平均值,我可以圍繞平均值繪製方差,但看起來不夠優雅,並且會使輸出雜亂Here dashed yellow line is the variance and red line is the mean

是否有任何其他方式來繪製密度函數?

我用這樣的事情:

mu = [mu1, mu2, mu3..] 
sigma = [sigma1, sigma2, sigma3..] 
variance1 = [mu1+sigma1, mu2+sigma2, ..] 
variance2 = [mu1-sigma1, mu2-sigma2,..] 


import matplotlib.pyplot as plt 
plt.plot(x,mu) 
plt.plot(x,variance1, ls = "--") 
plt.plot(x,variance2,ls="--") 

其中x是輸入數組。

+2

'imshow'? '吧?我們需要更多地瞭解您的圖表,其他元素以及示例的外觀。示例代碼作爲起點也會有所幫助 –

回答

3

最常用的方法是使用fill_between來遮蔽置信區間之間的區域。例如:

import numpy as np 
np.random.seed(1977) 
import matplotlib.pyplot as plt 

# Generate data... 
x_obs = np.linspace(-2, 2, 20) 
true_model = [0.2, -0.1, 4, 2, 1, 0] 

noise = np.random.normal(0, 5, x_obs.shape) 
y_obs = np.polyval(true_model, x_obs) + noise 

# Fit to a 5-th order polynomial 
fit_model = np.polyfit(x_obs, y_obs, 5) 

x = np.linspace(-3, 3, 100) 
y_true = np.polyval(true_model, x) 
y_pred = np.polyval(fit_model, x) 

# Made up confidence intervals (I'm too lazy to do the math...) 
high_bound = y_pred + 3 * (0.5 * x**4 + 3) 
low_bound = y_pred - 3 * (0.5 * x**4 + 3) 

# Plot the results... 
fig, ax = plt.subplots() 
ax.fill_between(x, high_bound, low_bound, color='gray', alpha=0.5) 
ax.plot(x_obs, y_obs, 'ko', label='Observed Values') 
ax.plot(x, y_pred, 'k--', label='Predicted Model') 
ax.plot(x, y_true, 'r-', label='True Model') 
ax.legend(loc='upper left') 
plt.show() 

enter image description here

+0

非常感謝。這正是我所期待的 –