gpt4 book ai didi

python - 将 2D bin 中散点值的平均值绘制为直方图/hexplot

转载 作者:行者123 更新时间:2023-12-04 12:05:58 24 4
gpt4 key购买 nike

我有 3 维分散数据 x, y, z。
我想将 x 和 y 的 bin 中 z 的平均值绘制为十六进制图或二维直方图。
是否有任何 matplotlib 函数可以做到这一点?
尽管这似乎是一个常见问题,但我只能提出一些非常繁琐的实现。

例如。像这样:

enter image description here

除了颜色应取决于 (x, y) bin 的平均 z 值(而不是 (x, y) bin 中的条目数,如默认的 hexplot/2D 直方图功能)。

最佳答案

如果分档是您要问的,那么 binned_statistic_2d可能对你有用。下面是一个例子:

from scipy.stats import binned_statistic_2d
import numpy as np

x = np.random.uniform(0, 10, 1000)
y = np.random.uniform(10, 20, 1000)
z = np.exp(-(x-3)**2/5 - (y-18)**2/5) + np.random.random(1000)

x_bins = np.linspace(0, 10, 10)
y_bins = np.linspace(10, 20, 10)

ret = binned_statistic_2d(x, y, z, statistic=np.mean, bins=[x_bins, y_bins])

fig, (ax0, ax1) = plt.subplots(1, 2, figsize=(12, 4))
ax0.scatter(x, y, c=z)
ax1.imshow(ret.statistic.T, origin='bottom', extent=(0, 10, 10, 20))

enter image description here

关于python - 将 2D bin 中散点值的平均值绘制为直方图/hexplot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58937863/

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