前言
本文推荐一个可以方便绘制日历热力图的库:calplot。
Calplot 是一个专门用于从 Pandas 时间序列数据生成热力图的 Python 包。该包起源于对现有项目 calmap 的分支开发,并在其基础上进行了创新性扩展。Calplot 添加了新的参数设置选项,旨在简化并增强用户对热力图样式的个性化定制过程。
安装
pip install calplot目前的版本是 0.1.7.5
示例 1:热力填色
all_days = pd.date_range('1/1/2023', periods=365, freq='D')
days = np.random.choice(all_days, 365)
events = pd.Series(np.random.randn(len(days)), index=days)
calplot.calplot(events,edgecolor='k',cmap='YlGn',yearlabel_kws={'color': 'tab:blue'})其中,我们需要输入的数据 events 是具有时间索引的数组,且索引并不需要严格遵守时间顺序
print(events)
----------------
2023-07-28 0.294124
2023-09-25 -0.624967
2023-08-28 1.038235
2023-03-02 0.987323
2023-06-07 -0.017341
...
2023-04-06 -0.307937
2023-02-10 -0.476559
2023-02-08 -0.564652
2023-12-22 0.596845
2023-11-22 -0.451508
Length: 200, dtype: float64示例 2:显示数值与图题
all_days = pd.date_range('1/1/2023', periods=365, freq='D')
days = np.random.choice(all_days, 200)
events = pd.Series(np.random.randn(len(days)), index=days)
calplot.calplot(events,edgecolor='k',cmap='YlGn',yearlabel_kws={'color': 'tab:blue'},textformat='{:.0f}', textfiller=' ',colorbar=False,suptitle='TEST')示例 3:多子图
all_days = pd.date_range('1/1/2023', periods=730, freq='D')
days = np.random.choice(all_days, 400)
events = pd.Series(np.random.randn(len(days)), index=days)
calplot.calplot(events)