<p>我不想使用np.hist2d,它通常会生成非常难看的直方图,而是希望循环使用<a href="https://github.com/alejandrobll/py-sphviewer">py-sphviewer</a>,这是一个python包,用于使用自适应平滑内核渲染粒子模拟,并且可以很容易地从pip安装(请参阅网页文档)。请考虑以下基于示例的代码:</p>
<pre><code>import numpy as np
import numpy.random
import matplotlib.pyplot as plt
import sphviewer as sph
def myplot(x, y, nb=32, xsize=500, ysize=500):
xmin = np.min(x)
xmax = np.max(x)
ymin = np.min(y)
ymax = np.max(y)
x0 = (xmin+xmax)/2.
y0 = (ymin+ymax)/2.
pos = np.zeros([3, len(x)])
pos[0,:] = x
pos[1,:] = y
w = np.ones(len(x))
P = sph.Particles(pos, w, nb=nb)
S = sph.Scene(P)
S.update_camera(r='infinity', x=x0, y=y0, z=0,
xsize=xsize, ysize=ysize)
R = sph.Render(S)
R.set_logscale()
img = R.get_image()
extent = R.get_extent()
for i, j in zip(xrange(4), [x0,x0,y0,y0]):
extent[i] += j
print extent
return img, extent
fig = plt.figure(1, figsize=(10,10))
ax1 = fig.add_subplot(221)
ax2 = fig.add_subplot(222)
ax3 = fig.add_subplot(223)
ax4 = fig.add_subplot(224)
# Generate some test data
x = np.random.randn(1000)
y = np.random.randn(1000)
#Plotting a regular scatter plot
ax1.plot(x,y,'k.', markersize=5)
ax1.set_xlim(-3,3)
ax1.set_ylim(-3,3)
heatmap_16, extent_16 = myplot(x,y, nb=16)
heatmap_32, extent_32 = myplot(x,y, nb=32)
heatmap_64, extent_64 = myplot(x,y, nb=64)
ax2.imshow(heatmap_16, extent=extent_16, origin='lower', aspect='auto')
ax2.set_title("Smoothing over 16 neighbors")
ax3.imshow(heatmap_32, extent=extent_32, origin='lower', aspect='auto')
ax3.set_title("Smoothing over 32 neighbors")
#Make the heatmap using a smoothing over 64 neighbors
ax4.imshow(heatmap_64, extent=extent_64, origin='lower', aspect='auto')
ax4.set_title("Smoothing over 64 neighbors")
plt.show()
</code></pre>
<p>生成以下图像:</p>
<p><a href="https://i.stack.imgur.com/4KwCN.png"><img src="https://i.stack.imgur.com/4KwCN.png" alt="enter image description here"/></a></p>
<p>如你所见,这些图像看起来非常漂亮,我们能够识别出它的不同子结构。这些图像是为特定区域内的每个点扩展给定的权重而构建的,该权重由平滑长度定义,而平滑长度又由到更接近的<em>nb</em>邻居的距离来确定(我选择了16、32和64作为示例)。因此,与低密度区域相比,高密度区域通常分布在较小的区域上。</p>
<p>函数myplot只是一个非常简单的函数,我编写这个函数是为了将x,y数据交给py sphviewer来完成这个魔术。</p>