Python 如何为 Seaborn 热图添加标题?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32723798/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 12:07:54  来源:igfitidea点击:

How do I add a title to Seaborn Heatmap?

pythonpandasipython-notebookseaborn

提问by Ammar Akhtar

I want to add a title to a seaborn heatmap. Using Pandas and iPython Notebook

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

code is below,

代码如下,

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

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

the data is pretty straight forward:

数据非常简单:

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

采纳答案by areuexperienced

heatmapis an axes-level function, so you should be able to use just plt.titleor ax.set_title:

heatmap是一个axes级别的函数,所以你应该可以只使用plt.titleor 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()

enter image description here

在此处输入图片说明

回答by Charlie

Alternatively sns.plt.suptitle('lalala')would work if you have multiple subplots.

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

回答by Rudra Mohan

To give title for seaborn heatmap use

为seaborn热图使用提供标题

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

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

或者 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()

Output >>>

输出 >>>

enter image description here

在此处输入图片说明