【问题标题】:How do I add a title and axis labels to Seaborn Heatmap?如何向 Seaborn Heatmap 添加标题和轴标签?
【发布时间】:2015-12-19 20:09:38
【问题描述】:

我想为 seaborn 热图添加标题。使用 Pandas 和 iPython Notebook

代码如下,

a1_p = a1.pivot_table( index='Postcode', columns='Property Type', values='Count', aggfunc=np.mean, fill_value=0)

sns.heatmap(a1_p, cmap="YlGnBu")

数据非常简单:

In [179]: a1_p

Out [179]:
Property Type   Flat    Terraced house  Unknown
Postcode            
E1  11  0   0
E14 12  0   0
E1W 6   0   0
E2  6   0   0

【问题讨论】:

  • 你试过plt.title()吗?

标签: python pandas jupyter-notebook seaborn


【解决方案1】:

给seaborn heatmap使用标题

plt.title("Enter your title", fontsize =20)

ax.set(title = "Enter your title")

import seaborn as sns # for data visualization
import matplotlib.pyplot as plt # for data visualization

flight = sns.load_dataset('flights') # load flights datset from GitHub seaborn repository

# reshape flights dataeset in proper format to create seaborn heatmap
flights_df = flight.pivot('month', 'year', 'passengers') 

ax = sns.heatmap(flights_df) # create seaborn heatmap


plt.title('Heatmap of Flighr Dataset', fontsize = 20) # title with fontsize 20
plt.xlabel('Years', fontsize = 15) # x-axis label with fontsize 15
plt.ylabel('Monthes', fontsize = 15) # y-axis label with fontsize 15

plt.show()

输出 >>>

【讨论】:

    【解决方案2】:

    如果您有多个子图,sns.plt.suptitle('lalala') 也可以使用。

    【讨论】:

    • Seaborn 在他们的包中放弃了对 matplotlib.pyplot 的曝光,导入 matplotlib.pyplot as plt 并改写 plt.suptitle('lalala')
    【解决方案3】:

    heatmap 是一个axes 级别的函数,所以你应该可以只使用plt.titleax.set_title

    %matplotlib inline
    import numpy as np
    import os
    import seaborn as sns
    import matplotlib.pyplot as plt
    
    data = np.random.randn(10,12)
    
    ax = plt.axes()
    sns.heatmap(data, ax = ax)
    
    ax.set_title('lalala')
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 2017-06-24
      • 2018-10-29
      • 1970-01-01
      • 2018-08-21
      • 2016-07-05
      • 2019-10-02
      • 1970-01-01
      • 2020-09-14
      • 1970-01-01
      相关资源
      最近更新 更多