绘制显示颗粒百分比的轮廓线 [英] Plotting contour lines that show percentage of particles
问题描述
我试图生产的东西与此情节相似:
What I am trying to produce is something similar to this plot:
这是一个等高线图,代表两个数据集中包含的粒子的68%,95%,99.7%.
Which is a contour plot representing 68%, 95%, 99.7% of the particles comprised in two data sets.
到目前为止,我已经尝试实现高斯KDE估计,并将这些高斯粒子绘制在轮廓上.
So far, I have tried to implement a gaussain KDE estimate, and plotting those particles gaussians on a contour.
文件添加到此处 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()
产生这个:
] 在1)我不知道如何一定要控制高斯kde中的bin数的情况下,2)轮廓标签都为零,3)我不知道确定百分位数的原因.
Where 1) I do not know how to necessarily control the bin number in gaussain kde, 2) The contour labels are all zero, 3) I have no clue on determining the percentiles.
感谢您的帮助.
推荐答案
摘自以下 matplotlib 文档
您可以将数据zi转换为百分比刻度(0-1),然后转换等高线图.
you can transform your data zi to a percentage scale (0-1) and then contour plot.
您还可以在调用plt.contour()时手动确定计数图的级别.
You can also manually determine the levels of the countour plot when you call plt.contour().
下面是一个具有2个随机生成的正态双变量分布的示例:
Below is an example with 2 randomly generated normal bivariate distributions:
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)
使用您提供的代码中的数据同样有效:
Using the data you provided the code works just as well:
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)
这篇关于绘制显示颗粒百分比的轮廓线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!