【问题标题】:Plotting contour lines that show percentage of particles绘制显示粒子百分比的等高线
【发布时间】:2017-11-30 17:42:42
【问题描述】:

我想要制作的是类似于这个情节的东西:

这是一个等高线图,代表了两个数据集中包含的 68%、95%、99.7% 的粒子。

到目前为止,我已经尝试实现高斯 KDE 估计,并将这些粒子高斯分布在轮廓上。

文件添加到这里https://www.dropbox.com/sh/86r9hf61wlzitvy/AABG2mbmmeokIiqXsZ8P76Swa?dl=0

from scipy.stats import gaussian_kde
import matplotlib.pyplot as plt
import numpy as np

# My data
x = RelDist
y = RadVel

# Peform the kernel density estimate
k = gaussian_kde(np.vstack([RelDist, RadVel]))
xi, yi = np.mgrid[x.min():x.max():x.size**0.5*1j,y.min():y.max():y.size**0.5*1j]
zi = k(np.vstack([xi.flatten(), yi.flatten()]))



fig = plt.figure()
ax = fig.gca()


CS = ax.contour(xi, yi, zi.reshape(xi.shape), colors='darkslateblue')
plt.clabel(CS, inline=1, fontsize=10)

ax.set_xlim(20, 800)
ax.set_ylim(-450, 450)
ax.set_xscale('log')

plt.show()

制作这个:

]2

其中 1) 我不知道如何控制 gaussain kde 中的 bin 编号,2) 轮廓标签全为零,3) 我不知道确定百分位数。

感谢任何帮助。

【问题讨论】:

  • 你有任何样本数据来重现你的情节吗?
  • @Tkanno 我已经添加了
  • 导致 Dropbox 链接损坏@DarthLazar
  • 过失,我修好了。

标签: python arrays numpy matplotlib contour


【解决方案1】:

取自 example in the matplotlib 文档

您可以将数据 zi 转换为百分比刻度 (0-1),然后绘制等高线图。

您还可以在调用 plt.contour() 时手动确定计数图的水平。

下面是一个包含 2 个随机生成的正态双变量分布的示例:

delta = 0.025
x = y = np.arange(-3.0, 3.01, delta)
X, Y = np.meshgrid(x, y)
Z1 = plt.mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
Z2 = plt.mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
Z = 10* (Z1- Z2)

#transform zi to a 0-1 range
Z = Z = (Z - Z.min())/(Z.max() - Z.min())

levels =  [0.68, 0.95, 0.997] 
origin = 'lower'
CS = plt.contour(X, Y, Z, levels,
              colors=('k',),
              linewidths=(3,),
              origin=origin)

plt.clabel(CS, fmt='%2.3f', colors='b', fontsize=14)

使用您提供的数据,代码也同样有效:

from scipy.stats import gaussian_kde
import matplotlib.pyplot as plt
import numpy as np

RadVel = np.loadtxt('RadVel.txt')
RelDist = np.loadtxt('RelDist.txt')
x = RelDist
y = RadVel

k = gaussian_kde(np.vstack([RelDist, RadVel]))
xi, yi = np.mgrid[x.min():x.max():x.size**0.5*1j,y.min():y.max():y.size**0.5*1j]
zi = k(np.vstack([xi.flatten(), yi.flatten()]))

#set zi to 0-1 scale
zi = (zi-zi.min())/(zi.max() - zi.min())
zi =zi.reshape(xi.shape)

#set up plot
origin = 'lower'
levels = [0,0.1,0.25,0.5,0.68, 0.95, 0.975,1]

CS = plt.contour(xi, yi, zi,levels = levels,
              colors=('k',),
              linewidths=(1,),
              origin=origin)

plt.clabel(CS, fmt='%.3f', colors='b', fontsize=8)
plt.gca()
plt.xlim(10,1000)
plt.xscale('log')
plt.ylim(-200,200)

【讨论】:

    猜你喜欢
    • 2016-10-19
    • 1970-01-01
    • 1970-01-01
    • 2012-07-14
    • 1970-01-01
    • 2016-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多