如何将标题移到热图的底部

时间:2020-07-03 12:55:44

标签: python matplotlib seaborn heatmap

我有以下代码可创建热图:

import matplotlib.pyplot as plt
import seaborn as sns

# Load dataset from seaborn
flights = sns.load_dataset('flights')
flights = flights.pivot('month', 'year', 'passengers')

# Plot heatmap
fig1, ax1 = plt.subplots(figsize=(8,8))
ax = sns.heatmap(flights, annot=True, fmt='d', cbar_kws={'label': 'Passenger Numbers'})

# Rotate x-axis labels
ax1.set_xticklabels(ax1.get_xticklabels(), rotation=45)

# Set font size for colorbar title
ax1.figure.axes[-1].yaxis.label.set_size(12)
# Set font size for colorbar labels
ax1.figure.axes[-1].tick_params(labelsize=10)

# Set font for x-axis labels
plt.xticks(fontsize=12, fontstyle='oblique')
# Set font for y-axis labels
plt.yticks(fontsize=12, fontstyle='oblique')
# Set title
plt.title('Heatmap Title', fontsize=39, weight='bold')   # What needs to change here?  

# Bold 'February' in y-axis
for label in ax.get_yticklabels():
    if label.get_text() == 'February':
        label.set_weight('bold')

plt.show()

如何移动标题,使其位于热图的底部?我尝试通过使用y=-0.01来遵循此other post,但似乎不起作用吗?

1 个答案:

答案 0 :(得分:2)

ax.text(x,y,z)您可以使用'set_title()和'plt.title'仅设置标题的顶部,左侧,右侧和中心,但也可以使用ax.text(x,y,z)设置标题。您可以使用ax.textax.annotation

# plt.title('Heatmap Title', fontsize=39, weight='bold')   # What needs to change here?  
ax.text(1, 14, 'Heatmap Title',fontsize=39, weight='bold')

enter image description here