python matplotlib - 等高线图 - 置信区间

时间:2012-06-09 18:56:26

标签: python statistics matplotlib

我正在尝试使用matplotlib.pyplot.contour在数据网格上绘制轮廓(可行),但轮廓位于远离峰值的1,2和3西格玛处。除了暴力之外,还有一种巧妙的方法吗?谢谢!

Python版本

Python 2.7.2 | EPD 7.2-2(64位)| (默认,2011年9月7日,16:31:15) [dclwin上的[GCC 4.0.1(Apple Inc. build 5493)]

1 个答案:

答案 0 :(得分:6)

您可以指定绘制轮廓的z-values列表。因此,您所要做的就是为您的发行版收集正确的z-values。以下是远离峰值的'1,2和3西格玛'的示例:

enter image description here

代码:

import numpy as np
import matplotlib.cm as cm
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt

#Set up the 2D Gaussian:
delta = 0.025
x = np.arange(-3.0, 3.0, delta)
y = np.arange(-3.0, 3.0, delta)
X, Y = np.meshgrid(x, y)
sigma = 1.0
Z = mlab.bivariate_normal(X, Y, sigma, sigma, 0.0, 0.0)
#Get Z values for contours 1, 2, and 3 sigma away from peak:
z1 = mlab.bivariate_normal(0, 1 * sigma, sigma, sigma, 0.0, 0.0)
z2 = mlab.bivariate_normal(0, 2 * sigma, sigma, sigma, 0.0, 0.0)
z3 = mlab.bivariate_normal(0, 3 * sigma, sigma, sigma, 0.0, 0.0)

plt.figure()
#plot Gaussian:
im = plt.imshow(Z, interpolation='bilinear', origin='lower',
                 extent=(-50,50,-50,50),cmap=cm.gray)
#Plot contours at whatever z values we want:
CS = plt.contour(Z, [z1, z2, z3], origin='lower', extent=(-50,50,-50,50),colors='red')
plt.savefig('fig.png')
plt.show()