在这里深入研究 calalmap 代码卡尔马特 https://github.com/martijnvermaat/calmap/blob/master/calmap/__init__.py我明白那个
- calendarplot 为每个子图调用多个年份图(在您的情况下调用两次)
- 年情节创建第一个
ax.pcolormesh
包含背景,然后是另一个包含实际数据的内容,再加上一堆其他内容。
要深入研究与轴相关的对象,您可以使用(我假设您的代码导入和数据初始化在此处之前进行):
ax[0].get_children()
[<matplotlib.collections.QuadMesh at 0x11ebd9e10>,
<matplotlib.collections.QuadMesh at 0x11ebe9210>, <- that's the one we need!
<matplotlib.spines.Spine at 0x11e85a910>,
<matplotlib.spines.Spine at 0x11e865250>,
<matplotlib.spines.Spine at 0x11e85ad10>,
<matplotlib.spines.Spine at 0x11e865490>,
<matplotlib.axis.XAxis at 0x11e85a810>,
<matplotlib.axis.YAxis at 0x11e74ba90>,
<matplotlib.text.Text at 0x11e951dd0>,
<matplotlib.text.Text at 0x11e951e50>,
<matplotlib.text.Text at 0x11e951ed0>,
<matplotlib.patches.Rectangle at 0x11e951f10>]
现在,我们可以使用fig.colorbar
(plt.colorbar
是这个函数的包装器)如这个答案中所建议的Matplotlib 2 个子图,1 个颜色条 https://stackoverflow.com/a/23795901/1435167 :
fig,ax=calmap.calendarplot(df['data'],
fillcolor='grey', linewidth=0,cmap='RdYlGn',
fig_kws=dict(figsize=(17,8)))
fig.colorbar(ax[0].get_children()[1], ax=ax.ravel().tolist())
这会产生一个垂直颜色条,仅在第一个图中引用颜色,但所有图的颜色都是相同的。
我仍在尝试更好的轴位置和水平位置,但从这里开始应该很容易。
作为奖励,对于单个年份图:
fig = plt.figure(figsize=(20,8))
ax = fig.add_subplot(111)
cax = calmap.yearplot(df, year=2014, ax=ax, cmap='YlGn')
fig.colorbar(cax.get_children()[1], ax=cax, orientation='horizontal')