2016-03-08 56 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