2016-03-08 138 views
1

这是我第一篇文章!向calmap绘图添加颜色条

我使用calmap来绘制漂亮的日历图来分析一些数据。日历图使用颜色表来显示几天之间的对比。我遇到的问题是,calmap不提供友好的工具来显示与日历图关联的颜色条。我想知道你们中的一个是否有解决方案。理想的做法是将颜色条设置为整个图形而不是一个轴。

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) 

的calmap情节例如

enter image description here

回答

0

挖掘到calmap代码在这里martijnvermaat/calmap我明白

  • calendarplot要求几个yearplot每subplot(在你的情况下两次)
  • yearplot创建第一个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.colorbarplt.colorbar是一个包装:

钻研有关你可以使用一个轴对象(我假设你的代码导入和数据初始化任何事情之前这里)解决此功能)在此答案 Matplotlib 2 Subplots, 1 Colorbar建议:

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()) 

这产生垂直colobar参考颜色只在第一个情节中,但所有情节的颜色都是相同的。

enter image description here

我仍然与位置更好的轴和横向一玩,但它应该很容易从这里。

至于奖金,对于单个yearplot:

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') 

enter image description here

+0

它完美。感谢您提供简单而干净的解决方案 – ABreit

+0

听起来很棒:-D – kidpixo