向镇定图添加颜色条 [英] add a colorbar to a calmap plot

查看:50
本文介绍了向镇定图添加颜色条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在这里的第一篇文章!

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

calmap 的文档:

解决方案

在此处挖掘镇定代码

我仍在沿轴和水平方向更好的位置玩游戏,但是从这里开始应该很容易.

作为奖金,用于一年的绘图:

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,direction ='horizo​​ntal')

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

解决方案

Digging into calmap code here martijnvermaat/calmap I understand that

  • calendarplot calls several yearplot for each subplot (twice in your case)
  • yearplot create a first ax.pcolormesh with background and then another one with the actual data, plus a bunch of other thing.

To dig into the object related to an axis you can use (I assume your code import and data initialization before anything here) :

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>]

now, we can use fig.colorbar (plt.colorbar is a wrapper around this function) as suggested in this answer 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())

this produces a vertical colobar referencing color the only in the first plot, but the color are the same for all the plots.

I'm still playing with position better the axis and an horizontal one, but it should be easy from here.

As bonus, for a single 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')

这篇关于向镇定图添加颜色条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆