Python 中的数密度等值线

发布于 2024-11-24 04:28:24 字数 325 浏览 0 评论 0原文

我试图在 python 中重现这个图,但运气不佳:

在此处输入图像描述

这是当前完成的一个简单的数字密度轮廓在 SuperMongo 中。我想放弃它,转而使用Python,但我能得到的最接近的是:

在此处输入图像描述

这是使用 hexbin()。我怎样才能让 python 情节类似于 SuperMongo 情节?我没有足够的代表来发布图片,抱歉链接。感谢您抽出时间!

I'm trying to reproduce this plot in python with little luck:

enter image description here

It's a simple number density contour currently done in SuperMongo. I'd like to drop it in favor of Python but the closest I can get is:

enter image description here

which is by using hexbin(). How could I go about getting the python plot to resemble the SuperMongo one? I don't have enough rep to post images, sorry for the links. Thanks for your time!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

倦话 2024-12-01 04:28:24

来自 SuperMongo 同事的简单等高线图示例 => 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()

生成此图:< img src="https://i.sstatic.net/sMch8.png" alt="调整级别的等高线图">

最后,在 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()

这会生成一个看起来与线性刻度非常相似的图,但具有一个很好的垂直对数轴,这就是意图: “等高线图对数轴"

Example simple contour plot from a fellow SuperMongo => python sufferer:

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

produces a nice contour plot.

The contour function offers a lot of fancy adjustments, for example let's set the levels by hand:

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

producing this plot:contour plot with adjusted levels

And finally, in SM one can do contour plots on linear and log scales, so I spent a little time trying to figure out how to do this in matplotlib. Here is an example when the y points need to be plotted on the log scale and the x points still on the linear scale:

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

This produces a plot which looks very similar to the linear one, but with a nice vertical log axis, which is what was intended: contour plot with log axis

腻橙味 2024-12-01 04:28:24

不幸的是我无法查看你的图片。你的意思是像这个吗?它是由 MathGL —— GPL 绘图库完成的,它也有 Python 接口。您可以使用任意数据数组作为输入(包括 numpy 的数组)。

Unfortunately I couldn't view yours images. Do you mean something like this? It was done by MathGL -- GPL plotting library, which have Python interface too. And you can use arbitrary data arrays as input (including numpy's one).

漫雪独思 2024-12-01 04:28:24

您可以使用 numpy.histogram2d 来获取数组的数密度分布。
试试这个例子:
http://micropore.wordpress.com/ 2011/10/01/二维密度图或二维直方图/

You can use numpy.histogram2d to get a number density distribution of your array.
Try this example:
http://micropore.wordpress.com/2011/10/01/2d-density-plot-or-2d-histogram/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文