Python中的数字密度轮廓

2024-06-17 18:31:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用python重现这个情节,但运气不好:

enter image description here

这是一个简单的数字密度轮廓目前在超长。我想放弃使用Python,但我能得到的最接近的是:

enter image description here

这是通过使用hexbin()实现的。我怎么能让Python的情节和超人的相似呢?我没有足够的代表张贴图片,抱歉的链接。谢谢你的时间!


Tags: 链接时间图片代表数字轮廓密度情节
3条回答

你结帐了吗matplotlib's contour plot

很不幸我看不到你的照片。你是说像this这样的东西吗?这是由MathGL--GPL绘图库完成的,它也有Python接口。您可以使用任意数据数组作为输入(包括numpy的数组)。

来自一位超级Python患者的简单等高线图示例:

import numpy as np
from matplotlib.colors import LogNorm
from matplotlib import pyplot as plt

plt.interactive(True)
fig=plt.figure(1)
plt.clf()

# generate input data; you already have that
x1 = np.random.normal(0,10,100000)
y1 = np.random.normal(0,7,100000)/10.
x2 = np.random.normal(-15,7,100000)
y2 = np.random.normal(-10,10,100000)/10.
x=np.concatenate([x1,x2])
y=np.concatenate([y1,y2])

# calculate the 2D density of the data given
counts,xbins,ybins=np.histogram2d(x,y,bins=100,normed=LogNorm())
# make the contour plot
plt.contour(counts.transpose(),extent=[xbins.min(),xbins.max(),
    ybins.min(),ybins.max()],linewidths=3,colors='black',
    linestyles='solid')
plt.show()

产生一个很好的等高线图。

轮廓函数提供了许多奇特的调整,例如,让我们手动设置级别:

plt.clf()
mylevels=[1.e-4, 1.e-3, 1.e-2]
plt.contour(counts.transpose(),mylevels,extent=[xbins.min(),xbins.max(),
    ybins.min(),ybins.max()],linewidths=3,colors='black',
    linestyles='solid')
plt.show()

生成此绘图:contour plot with adjusted levels

最后,在SM中,可以在线性和对数刻度上绘制等高线图,所以我花了一点时间在matplotlib中尝试如何实现这一点。下面是需要在对数刻度上绘制y点而x点仍在线性刻度上的示例:

plt.clf()
# this is our new data which ought to be plotted on the log scale
ynew=10**y
# but the binning needs to be done in linear space
counts,xbins,ybins=np.histogram2d(x,y,bins=100,normed=LogNorm())
mylevels=[1.e-4,1.e-3,1.e-2]
# and the plotting needs to be done in the data (i.e., exponential) space
plt.contour(xbins[:-1],10**ybins[:-1],counts.transpose(),mylevels,
    extent=[xbins.min(),xbins.max(),ybins.min(),ybins.max()],
    linewidths=3,colors='black',linestyles='solid')
plt.yscale('log')
plt.show()

这会产生一个与线性曲线非常相似的曲线图,但是有一个很好的垂直对数轴,这就是我们想要的:contour plot with log axis

相关问题 更多 >