Matplotlib Hexbin颜色条显示异常数值问题求助
Matplotlib Hexbin颜色条显示异常数值问题求助
大家好,我最近在尝试复刻Kirk Goldsberry的NBA投篮质量可视化图,用来展示球员投篮区域相对联盟平均水平的表现。
我已经完成了数据清洗,下面是我的数据集示例:
| PLAYER_NAME | BASIC_ZONE | LOC_X | LOC_Y | Relative Percentage |
|---|---|---|---|---|
| DeMar DeRozan | Mid-Range | 71.0 | 151.0 | 0.021217 |
| DeMar DeRozan | Mid-Range | -16.0 | 199.0 | 0.021217 |
| DeMar DeRozan | Mid-Range | -25.0 | 65.0 | 0.021217 |
| DeMar DeRozan | Mid-Range | -1.0 | 181.0 | 0.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
相关产品推荐
相关产品推荐

