如何为Seaborn Heatmap添加标题?

Amm*_*tar 27 python pandas ipython-notebook seaborn

我想为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")
Run Code Online (Sandbox Code Playgroud)

数据非常简单:

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
Run Code Online (Sandbox Code Playgroud)

are*_*ced 44

heatmap is an axes-level function, so you should be able to use just plt.title or ax.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()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


小智 22

为seaborn热图使用提供标题

plt.title("Enter your title", fontsize =20)
Run Code Online (Sandbox Code Playgroud)

或者 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()
Run Code Online (Sandbox Code Playgroud)

输出 >>>

在此处输入图片说明


Cha*_*lie 6

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

  • Seaborn 在其包中放弃了对 matplotlib.pyplot 的暴露,导入 `matplotlib.pyplot as plt` 并编写 `plt.suptitle('lalala')` 来代替 (2认同)