python - Matplotlib hexbin : How to avoid displaying bins where np. 平均值为零

标签 python matplotlib

这与设置 mincnt=1 不同。当单元格的平均值为零时是否可以删除彩色箱?请参阅下面的代码示例。我希望像 1 这样的点不要被绘制。设置 mincnt=1 将删除单元格中出现一次且平均值 >=0

的点
d1 = {'Size': {0: 0, 1: 0,2: 0, 3: 5, 4: 3, 5: 3, 6: 8, 7: 5, 8: 9, 9: 3, 10: 6, 11: 7, 12: 5, 13: 6, 14: 5, 15: 5, 16: 7, 17: 6, 18: 0},
         'X': {0: -0.86602540400000005, 1: 0.86602540400000005, 2: 0.0, 3: -0.34641016200000002, 4: -0.28867513500000003, 5: 0.0, 6: 0.10825317499999999, 7: 0.34641016200000002, 8: 0.0, 9: -0.86602540400000005, 10: -0.43301270200000003, 11: 0.0, 12: -0.17320508100000001, 13: 0.43301270200000003, 14: 0.34641016200000002, 15: 0.34641016200000002, 16: 0.0, 17: 0.0, 18: 0.0},
         'Y': {0: -0.5, 1: -0.5, 2: 1.0, 3: 0.40000000000000002, 4: -0.5, 5: 1.0, 6: 0.0625, 7: 0.40000000000000002, 8: 0.0, 9: -0.5, 10: 0.25, 11: 0.14285714300000002, 12: -0.5, 13: 0.25, 14: 0.40000000000000002, 15: 0.40000000000000002, 16: 0.14285714300000002, 17: -0.5, 18: 0.0}}

test1 = pd.DataFrame(d1)
import numpy as np
plot_format = {
    "gridsize":15,
    "cmap":plt.cm.YlOrRd,
    #"mincnt":1,
    "vmin":1,
    "vmax":9,        
    "reduce_C_function":np.mean
}

ax = plt.subplot(111)
ax.hexbin(test1["X"], test1["Y"], C=test1.Size, **plot_format)
ax.axis([-1, 1, -0.8, 1.2])
plt.show()

最佳答案

您可以使用 hexbin 返回的 matplotlib.collections.PolyCollection 对象的 .get_array() 方法获取每个 bin 的计数,然后用它来设置色 block 表面颜色的 Alpha channel :

hb = ax.hexbin(test1["X"], test1["Y"], C=test1.Size, **plot_format)

# you might need to force a draw event here, otherwise `hb.get_colors()` can return
# incorrect values
ax.figure.canvas.draw()

counts = hb.get_array()         # get the counts (n,)
colors = hb.get_facecolors()    # get the facecolors of the patches (n, 4)
colors[:, 3] = counts > 0       # set the alpha channel to 0 where counts <= 0
hb.set_facecolors(colors)       # update the facecolors of the patches

关于python - Matplotlib hexbin : How to avoid displaying bins where np. 平均值为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33510632/

相关文章:

python - 这两行代码有什么区别?机器学习阵列

python - CartoPy 中绘制的等高线的插值方法

python - matplotlib 中带有日期时间的堆积直方图

python - apache上的python flask应用程序的随机问题

python - Python 中的持久终端 session

python - 正则表达式 unicode 字符

python - TypeError : array([ 0.]) 不可 JSON 序列化

python - 在大型 Python 代码库中从结果追溯到原因

python - 在 Seaborn boxplot 中设置传单(异常值)样式被忽略

python - 如何使图像和散点图具有相同的高度?