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

标签 python python-3.x heatmap seaborn

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

(1) 我想要每个单元格中的数据值,我不需要每张图片的轴标签。因此,我设置了xticklabels、yticklables和annot;但他们没有反射(reflect)在图中。我应该怎么做? (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')

enter image description here

最佳答案

(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 issuexticklabels = 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": "horizo​​ntal"} 是使颜色条水平的 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()

这会生成这张图片:

enter image description here


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

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] 然后使用此可映射对象和 [ 创建一个 plt.colorbar ax1,ax2] 作为 ax 参数。我希望它每次都能工作,它绘制图像:

关于python - 一个用于 seaborn 热图子图的水平颜色条和带有 xticklabels 的 Annot Issue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45122416/

相关文章:

python 字符串格式 %x 和 %#x 有什么区别(哈希标签十六进制?)

python - 如何在django上按时间过滤数据?

Python - 池不使用所有内核

Python:根据元素的子集将列表拆分为多个列表

python - 使用 aiohttp/asyncio 的异步 HTTP 调用失败并显示 "Cannot connect to host [Network is unreachable]"

python - 在 Python 中创建信息图

python - Keras ImageDataGenerator——作为 Save_Prefix 分类?

python - 元素更改后列表不会更改

google-maps - 如何将HeatMaps添加到Flutter Mobile应用程序

r - R中热图/聚类默认值的差异(热图与热图.2)?