【问题标题】:How do I add a Title to a Seaborn Clustermap?如何向 Seaborn Clustermap 添加标题?
【发布时间】:2018-08-21 14:25:27
【问题描述】:

我已经搜索过,但尚未发现如何向 Seaborn Clustermap 添加标题。

我试过了:

plt.title('Title',loc='center')

但这会将标题添加到图例中,而不是主 Clustermap。

我也尝试过,首先创建一个轴对象并为其添加标题(这似乎适用于热图),但问题是集群图没有绘制在这个轴对象上。它显然是在自己的对象上绘图。

感谢您的帮助...

【问题讨论】:

    标签: python charts visualization seaborn


    【解决方案1】:

    您可以使用.fig.suptitle('Your Title')

    想象一下来自 Seaborn 文档的 this example 集群图:

    import seaborn as sns
    iris = sns.load_dataset("iris")
    species = iris.pop("species")
    g = sns.clustermap(iris)
    

    你添加一个标题:

    g = sns.clustermap(iris).fig.suptitle('Your Title') 
    

    或者简单地说:

    g.fig.suptitle('Your Title') 
    

    但是,这会为整个图形而不是单个子图添加标题。如果要为子图添加标题,请参阅@Kiraly Sandor 的解决方案。

    g.fig.suptitle('Figure Title')
    g.ax_heatmap.set_title('Subplot Title')
    

    【讨论】:

    • 感谢您的全面回答。
    • 之后你可能还想用g.fig.subplots_adjust(top=.9)调整字幕的位置,否则剧情和标题之间往往会出现很大的丑陋差距。我也喜欢使用g.ax_col_dendrogram.set_title(),它将标题直接添加到顶部树状图上方。
    【解决方案2】:

    其中一个解决方案可能是:绘图返回的对象具有ax_heatmap 方法,该方法具有set_title 方法。如果你用标题设置它,你的情节将有一个:

    import seaborn as sns
    sns.set(color_codes=True)
    iris = sns.load_dataset("iris")
    species = iris.pop("species")
    g = sns.clustermap(iris)
    g.ax_heatmap.set_title('lalal')
    

    但是,这种方法会在您的热图上方添加一个标题,而不是整个图。

    【讨论】:

      猜你喜欢
      • 2020-11-03
      • 2016-04-06
      • 2015-12-19
      • 2019-01-19
      • 2017-07-13
      • 2020-09-14
      • 1970-01-01
      • 2018-02-28
      • 2015-07-01
      相关资源
      最近更新 更多