Python Matplotlib - 等高线图 - 置信区间

7
我正在尝试使用matplotlib.pyplot.contour在数据网格上绘制等高线(可行),但要将等高线放置在距离峰值1、2和3个标准差处。除了暴力解决办法之外,是否有更好的方法?谢谢!
Python版本为
Python 2.7.2 |EPD 7.2-2 (64-bit)| (default, Sep 7 2011, 16:31:15) [GCC 4.0.1 (Apple Inc. build 5493)] on darwin
1个回答

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接