pandas - Pandas/Matplotlib 中 Hexbin 颜色条的自定义刻度和标签
问题描述
如何为 Hexbin 图的颜色条创建自定义刻度和标签?
解决方案
修改颜色条的关键是获得对它的访问权限,然后使用定位器和格式化程序(在matplotlib.ticker中)来修改它们,最后在更改完成后更新刻度。
import matplotlib.pyplot as plt
import matplotlib.ticker as tkr
# set the values of the main y axis and colorbar ticks
ticklocs = tkr.FixedLocator( np.linspace( ymin, ymax, ystep ) )
ticklocs2 = tkr.FixedLocator( np.arange( zmin, zmax, zstep ) )
# set the label format for the main y axis, and colorbar
tickfrmt = tkr.StrMethodFormatter( yaxis_format )
tickfrmt2 = tkr.StrMethodFormatter( zaxis_format )
# must save a reference to the plot in order to retrieve its colorbar
hb = df.plot.hexbin(
'xaxis', 'yaxis', C = 'zaxis',
sharex = False
)
# get plot axes, ax[0] is main axes, ax[1] is colorbar
ax = plt.gcf().get_axes()
# get colorbar
cbar = hb.collections[ 0 ].colorbar
# set main y axis ticks and labels
ax[ 0 ].yaxis.set_major_locator( ticklocs )
ax[ 0 ].yaxis.set_major_formatter( tickfrmt )
# set color bar ticks and labels
cbar.locator = ticklocs2
cbar.formatter = tickfrmt2
cbar.update_ticks() # update ticks for changes to take place
(这个问题给我带来了一些重大问题,所以我想我会分享如何完成这个。)
推荐阅读
- powershell - 为什么 PowerShell 会在 Linux 作为第三台计算机时出现双跳问题
- javascript - 什么算法最适合对表格的单元格进行分组?
- c - .txt 文件中的行是否正好有 300 个字符?
- python - 使用 Pandas 数据框的 SQL Server 合并?
- python - 如何让用户在 Django 中编辑/更新 .py 文件?
- flutter - Flutter ListView 在底层列表更新时不更新
- reactjs - 尚未单击按钮时显示 NaN 的 React 应用程序
- javascript - 如何创建包含大量本地数据的快速用户界面?
- python-3.x - ttk.Widget 自定义条目,条目不可编辑或不可点击
- javascript - 使用脚本在 laravel 中使用模态形式更新/编辑