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

标签 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) [GCC 4.0.1 (Apple Inc. build 5493)] 在 Darwin 上

最佳答案

您可以指定绘制等高线的z 值 列表。因此,您所要做的就是为您的分布收集正确的 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()

关于python matplotlib - 等高线图 - 置信区间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10963709/

相关文章:

python - 在 matplotlib 中绘制 3d imshow() 图像

从字典中提取多个数组并用 numpy 堆叠它们的 Pythonic 方法

python - django.db.utils.IntegrityError : The row in table 'main_page_projects' with primary key '1' has an invalid foreign key 错误

python - 如何将数据从 ModelForm 保存到 Django 中的数据库?

php - 使用 PHP 生成统计信息(页面浏览量、唯一访问者)?

两个不等长分布的 Python Q-Q 和 P-P 图

python - 如何在matplotlib上生成一系列直方图?

python - 自定义排列,对等分布

python - Python 中频率权重的排名相关性

matplotlib 未检测到字体