一个用于 seaborn 热图子图的水平颜色条和带有 xticklabels 的 Annot Issue

One horizontal colorbar for seaborn heatmaps subplots and Annot Issue with xticklabels

我试着在一张图中写了多个热图。 我写了下面的代码,我有两个问题。

(1) 我想要每个单元格中的数据值,我不需要每张图片的轴标签。因此,我设置了xticklabels、yticklables和annot;但他们没有反映在图中。我应该怎么做? (2) 我可以旋转彩条吗?我需要一个水平颜色条用于此图。

我在 Ubuntu 14.04.5 LTS 中使用 Python 3.5.2。

import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np

%matplotlib notebook

flights = sns.load_dataset("flights")
flights = flights.pivot("month", "year", "passengers")
fig = plt.figure(figsize=(15, 8))
# integral
plt.subplot(1,2,1)
sns.set(font_scale=0.8)
plt.title('integral', fontsize = 1)
plt.subplots_adjust(top=0.90, left = 0.1)
sns.heatmap(flights, fmt='d', cmap='gist_gray_r', xticklabels = False, yticklabels = False, annot=True)

#float
plt.subplot(1,2,2)
sns.set(font_scale=0.8)
plt.title('float', fontsize = 1)
plt.subplots_adjust(top=0.90, left = 0.1)
sns.heatmap(flights, annot=True, fmt='.2f', cmap='gist_gray_r', xticklabels = False, yticklabels = False)

fig.suptitle('Title for figure', fontsize=20)
plt.subplots_adjust(top=0.9, left=0.06, bottom=0.08) #後ろ2つ追加
#x label
fig.text(0.5, 0.02, 'year', ha='center', va='center')
#y label
fig.text(0.02, 0.5, 'month', ha='center', va='center', rotation='vertical')

sns.plt.savefig('heatmap.png')

(1) I want the data value in each cell and I don't need the axis labels for each picture. Therefore, I set xticklabels, yticklables, and annot; but they were not reflected in the figure. How should I do?

这是一个recent fixed issue,当xticklabels = Falseyticklabels = False时,annot = True不起作用。解决方法是将 xticklabelsyticklabels 都设置为空字符串列表 [""].

我做了一个调整,用 fig, (ax1, ax2) = plt.subplots(1, 2, sharex=True, sharey=True) 声明子图轴,这更利于理解代码。我将所有轴标签设置为 "",例如:ax1.set_ylabel(''),因此在清理后,我们可以制作我们想要的标签,而不是使用 sns.heatmap 自动生成的标签。另外,图中的标签用这种方式生成的比使用fig.text.

手动设置的要好

(2) Can I rotate the color bar?

cbar_kws={"orientation": "horizontal"} 是使颜色条水平的 sns.heatmap 的参数。

使用下面的代码:

import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np

flights = sns.load_dataset("flights")
flights = flights.pivot("month", "year", "passengers")

fig, (ax1, ax2) = plt.subplots(1, 2, sharex=True, sharey=True)

#First

sns.heatmap(flights, ax=ax1, fmt='d', cmap='gist_gray_r', xticklabels = [""], yticklabels = [""], annot = True, cbar_kws={"orientation": "horizontal"})
ax1.set_ylabel('')    
ax1.set_xlabel('')
ax1.set_title('Integral')

#Second

sns.heatmap(flights, ax=ax2, fmt='.2f', cmap='gist_gray_r', xticklabels = [""], yticklabels = [""], annot = True, cbar_kws={"orientation": "horizontal"})
ax2.set_ylabel('')    
ax2.set_xlabel('')
ax2.set_title('Float')

ax1.set_ylabel("Month")
ax1.set_xlabel("Year")
ax2.set_xlabel("Year")

plt.show()

这会生成这张图片:


如果您希望只有一个大的水平颜色条,您可以将代码更改为以下内容:

import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np

flights = sns.load_dataset("flights")
flights = flights.pivot("month", "year", "passengers")

fig, (ax1, ax2) = plt.subplots(1, 2, sharex=True, sharey=True)

#First

im = sns.heatmap(flights, ax=ax1, fmt='d', cmap='gist_gray_r', xticklabels = [""], yticklabels = [""], annot = True, cbar = False)
ax1.set_ylabel('')    
ax1.set_xlabel('')
ax1.set_title('Integral')

#Second

sns.heatmap(flights, ax=ax2, fmt='.2f', cmap='gist_gray_r', xticklabels = [""], yticklabels = [""], annot = True, cbar = False)
ax2.set_ylabel('')    
ax2.set_xlabel('')
ax2.set_title('Float')

ax1.set_ylabel("Month")
ax1.set_xlabel("Year")
ax2.set_xlabel("Year")

mappable = im.get_children()[0]
plt.colorbar(mappable, ax = [ax1,ax2],orientation = 'horizontal')

plt.show()

我们正在获取可映射对象:mappable = im.get_children()[0],然后使用此可映射对象和 [ax1,ax2] 作为 ax 参数创建 plt.colorbar。我希望它每次都能工作,它绘制图像: