【问题标题】:add a colorbar to a calmap plot将颜色条添加到 Calmap 图
【发布时间】:2016-06-22 15:14:01
【问题描述】:

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

我正在使用 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 图示例

【问题讨论】:

    标签: python-2.7 matplotlib plot


    【解决方案1】:

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

    • calendarplot 为每个子图调用若干年图(在您的情况下为两次)
    • yearplot 创建第一个带有背景的ax.pcolormesh,然后创建另一个带有实际数据的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 引用颜色,但所有图的颜色都相同。

    我仍在使用轴和水平轴更好的位置,但从这里开始应该很容易。

    作为奖励,对于单个年份情节:

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

    【讨论】:

    • 完美运行。感谢您提供简单干净的解决方案
    【解决方案2】:

    将@kidpixo 答案和来自@bogatron 的this answer 放在一起,我准备了一个“做显而易见的事情”的自包含示例,在calmap 的右侧创建一个颜色条并匹配它的高度。

    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)
    

    【讨论】:

      猜你喜欢
      • 2020-11-13
      • 2015-08-16
      • 2014-01-12
      • 2022-12-12
      • 2021-06-28
      • 2020-10-29
      • 1970-01-01
      • 1970-01-01
      • 2017-12-23
      相关资源
      最近更新 更多