如何向 Seaborn 热图添加标题和轴标签?

问题描述 投票:0回答:4

我想为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
python pandas matplotlib seaborn heatmap
4个回答
98
投票

heatmap
是一个
axes
级函数,因此您应该能够仅使用
plt.title
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()


36
投票

为 Seaborn 热图使用提供标题

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

输出>>>


8
投票

或者,如果您有多个子图,则

sns.plt.suptitle('lalala')
也可以。


0
投票

您可以直接使用 matplotlib.pyplot 库中的相同 xlabel 和 ylabel 以及标题。

import matplotlib.pyplot as plt
import seaborn as sn

sn.heatmap(cm, annot=True, fmt="d")
plt.xlabel("Enter the X axis")
plt.ylabel("Enter the Y axis")
plt.title("Enter the Title")
plt.show()

注意:如果您需要更大的字体,请在所有这些中使用字体大小。

© www.soinside.com 2019 - 2024. All rights reserved.