【问题标题】:Seaborn Heatmap: Move colorbar on top of the plotSeaborn 热图:在绘图顶部移动颜色条
【发布时间】:2018-06-03 14:44:13
【问题描述】:

我有一个使用seaborn 库创建的基本热图,并且想要将颜色栏从默认的垂直和右侧移动到热图上方的水平颜色栏。我该怎么做?

以下是一些示例数据和默认示例:

import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np

# Create data
df = pd.DataFrame(np.random.random((5,5)), columns=["a","b","c","d","e"])

# Default heatma
ax = sns.heatmap(df)
plt.show()

【问题讨论】:

    标签: python python-2.7 pandas matplotlib seaborn


    【解决方案1】:

    查看the documentation,我们发现了一个参数cbar_kws。这允许指定传递给 matplotlib 的 fig.colorbar 方法的参数。

    cbar_kws:键的字典,值映射,可选。 fig.colorbar 的关键字参数。

    因此我们可以使用fig.colorbar 的任何可能参数,为cbar_kws 提供字典。

    在这种情况下,您需要 location="top" 将颜色条放在顶部。因为colorbar 默认情况下使用网格规范定位颜色条,因此不允许设置位置,我们需要关闭该网格规范​​ (use_gridspec=False)。

    sns.heatmap(df, cbar_kws = dict(use_gridspec=False,location="top"))
    

    完整示例:

    import matplotlib.pyplot as plt
    import seaborn as sns
    import pandas as pd
    import numpy as np
    
    df = pd.DataFrame(np.random.random((5,5)), columns=["a","b","c","d","e"])
    
    ax = sns.heatmap(df, cbar_kws = dict(use_gridspec=False,location="top"))
    
    plt.show()
    

    【讨论】:

    • 对于像我这样真正想把颜色条放在底部而不是location="top"的人,添加orientation="horizontal"
    【解决方案2】:

    我想展示带有子图的示例,它允许控制图的大小以保留热图的方形几何形状。这个例子很短:

    import matplotlib.pyplot as plt
    import seaborn as sns
    import pandas as pd
    import numpy as np
    
    # Create data
    df = pd.DataFrame(np.random.random((5,5)), columns=["a","b","c","d","e"])
    
    # Define two rows for subplots
    fig, (cax, ax) = plt.subplots(nrows=2, figsize=(5,5.025),  gridspec_kw={"height_ratios":[0.025, 1]})
    
    # Draw heatmap
    sns.heatmap(df, ax=ax, cbar=False)
    
    # colorbar
    fig.colorbar(ax.get_children()[0], cax=cax, orientation="horizontal")
    
    plt.show()
    

    【讨论】:

      【解决方案3】:

      您必须使用轴分隔器将颜色条放在海底图形的顶部。寻找 cmets。

      import matplotlib.pyplot as plt
      import seaborn as sns
      import pandas as pd
      import numpy as np
      from mpl_toolkits.axes_grid1.axes_divider import make_axes_locatable
      from mpl_toolkits.axes_grid1.colorbar import colorbar
      
      # Create data
      df = pd.DataFrame(np.random.random((5,5)), columns=["a","b","c","d","e"])
      
      # Use axes divider to put cbar on top
      # plot heatmap without colorbar
      ax = sns.heatmap(df, cbar = False)
      # split axes of heatmap to put colorbar
      ax_divider = make_axes_locatable(ax)
      # define size and padding of axes for colorbar
      cax = ax_divider.append_axes('top', size = '5%', pad = '2%')
      # make colorbar for heatmap. 
      # Heatmap returns an axes obj but you need to get a mappable obj (get_children)
      colorbar(ax.get_children()[0], cax = cax, orientation = 'horizontal')
      # locate colorbar ticks
      cax.xaxis.set_ticks_position('top')
      
      plt.show()
      

      欲了解更多信息,请阅读 matplotlib 的官方示例:https://matplotlib.org/gallery/axes_grid1/demo_colorbar_with_axes_divider.html?highlight=demo%20colorbar%20axes%20divider

      Heatmap 类似 sns.heatmap(df, cbar_kws = {'orientation':'horizontal'}) 的参数是无用的,因为它将颜色条放在底部位置。

      【讨论】:

      • 关于这里的最后一句话:你需要cbar_kws = {"location":"top", "use_gridspec" : False},如my answer所示。
      猜你喜欢
      • 1970-01-01
      • 2019-02-16
      • 1970-01-01
      • 2016-07-20
      • 2020-06-21
      • 2018-05-07
      • 2015-04-06
      • 2021-01-17
      • 2018-05-09
      相关资源
      最近更新 更多