向平静图添加颜色条

2024-03-27

这是我在此的头一篇博文!

我正在使用 calmap 绘制漂亮的日历图来分析一些数据。日历图使用颜色图来显示日期之间的对比。我遇到的问题是 calmap 没有提供友好的工具来显示与日历图相关的颜色条。我想知道你们中是否有人能解决这个问题。理想的情况是为整个图形设置颜色条,而不仅仅是一个轴。

calmap 的文档:http://pythonhosted.org/calmap/ http://pythonhosted.org/calmap/

import pandas as pd

import numpy as np

import calmap # pip install calmap

%matplotlib inline

df=pd.DataFrame(data=np.random.randn(500,1)
                ,index=pd.date_range(start='2014-01-01 00:00:00',freq='1D',periods =500)
                ,columns=['data'])

fig,ax=calmap.calendarplot(df['data'],
                    fillcolor='grey', linewidth=0,cmap='RdYlGn',
                    fig_kws=dict(figsize=(17,8)))

fig.suptitle('Calendar view' ,fontsize=20,y=1.08)

平静图示例


在这里深入研究 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')
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

向平静图添加颜色条 的相关文章

随机推荐