gpt4 book ai didi

python - 基于频率的 pyplot.hexbin 中的十六进制大小

转载 作者:太空宇宙 更新时间:2023-11-04 09:48:34 25 4
gpt4 key购买 nike

有没有办法根据频率增加 pyplot.hexbin 中六边形的相对大小?我只能看到关键字参数的 binsize,它会影响十六进制的数量,但不会影响它们的大小。

但是在this article (在“多变量六边形分箱”标题下的大约 2/3),它讨论了绘制大小与计数成正比的六边形,以便更清楚地观察趋势(请参见从该文章中截取的下图)

enter image description here

我是否错过了允许这样做的关键字参数?

谢谢!

最佳答案

对于所讨论的图,使用六边形合并没有任何优势,因为六边形具有不同的大小,因此不允许在整个图中进行一致的合并。但是,您可以使用恒定分箱并缩小没有最大值的单元格。

import matplotlib.pyplot as plt
from matplotlib.collections import PatchCollection
from matplotlib.path import Path
from matplotlib.patches import PathPatch
import numpy as np; np.random.seed(42)

a = np.random.rand(200,2)
b = np.random.rand(200)
c = np.concatenate((a,np.c_[b,b]), axis=0)

fig, ax = plt.subplots()

hexbin = ax.hexbin(c[:,0],c[:,1],gridsize=20, linewidth=0 )

def sized_hexbin(ax,hc):
offsets = hc.get_offsets()
orgpath = hc.get_paths()[0]
verts = orgpath.vertices
values = hc.get_array()
ma = values.max()
patches = []
for offset,val in zip(offsets,values):
v1 = verts*val/ma+offset
path = Path(v1, orgpath.codes)
patch = PathPatch(path)
patches.append(patch)

pc = PatchCollection(patches)
pc.set_array(values)
ax.add_collection(pc)
hc.remove()

sized_hexbin(ax,hexbin)
plt.show()

enter image description here


为了进行比较,可以通过散点图传输相同的信息。此代码可能比上面的更直观。

import matplotlib.pyplot as plt
import numpy as np; np.random.seed(42)

a = np.random.rand(200,2)
b = np.random.rand(200)
c = np.concatenate((a,np.c_[b,b]), axis=0)

fig, ax = plt.subplots()

h, ex, ey = np.histogram2d(c[:,0],c[:,1], bins=np.linspace(0,1,20))

X,Y = np.meshgrid(ex[:-1]+np.diff(ex)/2.,ey[:-1]+np.diff(ey)/2.)

ax.scatter(X.flatten(),Y.flatten(),
c=h.T.flatten(), s=h.T.flatten()**2,
marker="h")

plt.show()

enter image description here

关于python - 基于频率的 pyplot.hexbin 中的十六进制大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48844600/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com