You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Matplotlib Hexbin颜色条显示异常数值问题求助

Matplotlib Hexbin颜色条显示异常数值问题求助

大家好,我最近在尝试复刻Kirk Goldsberry的NBA投篮质量可视化图,用来展示球员投篮区域相对联盟平均水平的表现。

我已经完成了数据清洗,下面是我的数据集示例:

PLAYER_NAMEBASIC_ZONELOC_XLOC_YRelative Percentage
DeMar DeRozanMid-Range71.0151.00.021217
DeMar DeRozanMid-Range-16.0199.00.021217
DeMar DeRozanMid-Range-25.065.00.021217
DeMar DeRozanMid-Range-1.0181.00.021217

我用Matplotlib的hexbin函数生成可视化,代码如下:

#Plot Hex using MATPLOTLIB
fig, ax = plt.subplots()
hexbin = ax.hexbin(x=player_sorted.LOC_X, y=player_sorted.LOC_Y, C=player_sorted['Relative Percentage'], gridsize=25, bins=[-0.0875, -.0625,-0.0375,-0.0125, 0.0125, 0.0375, 0.0625, 0.0875],
           mincnt=5)#, vmin=-0.1, vmax=0.1)
cb = fig.colorbar(hexbin, ax=ax)
cb.set_label('Relative Percentage to League Average')
title_text = player + ' ' + str(year)
plt.title(title_text)
draw_court(ax)
# Adjust the axis limits and orientation of the plot in order
# to plot half court, with the hoop by the top of the plot
ax.set_xlim(-250,250)
ax.set_ylim(422.5, -47.5)

# Get rid of axis labels and tick marks
ax.set_xlabel('')
ax.set_ylabel('')
ax.tick_params(labelbottom='off', labelleft='off')

plt.show()

现在可视化的整体布局看起来没问题,但颜色条的数值完全不对。我原本期望颜色条显示每个六边形内所有点的Relative Percentage的均值(因为hexbin默认就是计算均值),数值应该在-0.1到+0.1之间,但实际显示的却是整数。我也检查过是不是默认变成求和了,但计算下来数值也对不上求和的结果,实在搞不懂哪里出问题了,有没有大佬能帮忙看看?

(附:当前生成的异常可视化图,图中颜色条显示整数而非预期的小数值)

备注:内容来源于stack exchange,提问作者mylesmoose

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.14 12:18:03