add a colorbar to a calmap plot

Viewed 3071

this is my first post here!

I'm using calmap to plot nice calendar plot to analyze some data. The calendar plot is using a colormap to show contrast between days. The problem I have is that calmap does not provide friendly tool to display a colorbar associated with the calendar plot. I would like to know if one of you have a solution to this. The ideal would be that the colorbar is set for the entire figure and not only one axis.

the documentation of 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)

the calmap plot example

enter image description here

2 Answers

Putting together @kidpixo answer and this answer from @bogatron, I prepared a self contained example that 'does the obvious thing', creating a colorbar on the right side of the calmap and matching its height.

import pandas as pd
import numpy as np
import calmap # pip install calmap
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
%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 = plt.figure(figsize=(20,8))
ax = fig.add_subplot(111)
cax = calmap.yearplot(df.data, year=2014, ax=ax, cmap='YlGn')
# fig.colorbar(cax.get_children()[1], ax=cax, orientation='horizontal')
divider = make_axes_locatable(cax)
lcax = divider.append_axes("right", size="2%", pad=0.5)
fig.colorbar(cax.get_children()[1], cax=lcax)

example calmap with side colorbar

Related