问题描述
我试图在一个图中编写多个热图.我写了下面的代码,我有两个问题.
I tried to write multiple heatmaps in one figure.I wrote the below code and I have two questions.
(1)我想要每个单元格中的数据值,并且不需要每个图片的轴标签.因此,我设置了xticklabels,yticklables和annot;但它们并未反映在图中.我应该怎么做?(2)我可以旋转彩条吗?为此,我需要一个水平色条.
(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?(2) Can I rotate the color bar? I need one horizontal color bar for this fifure.
我在Ubuntu 14.04.5 LTS中使用Python 3.5.2.
I use Python 3.5.2 in Ubuntu 14.04.5 LTS.
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')
推荐答案
这是一个最近已解决的问题,当xticklabels = False
或yticklabels = False
时, annot = True
不起作用.一种解决方法是将xticklabels
和yticklabels
都设置为空字符串[""]
的列表.
It's a recent fixed issue that when xticklabels = False
or yticklabels = False
, annot = True
doesn't work. A workaround is to set xticklabels
and yticklabels
both to a list of an empty string [""]
.
我做了一个调整,用fig, (ax1, ax2) = plt.subplots(1, 2, sharex=True, sharey=True)
声明了子图轴,这对于理解代码更好.我将所有轴标签都设置为""
,例如:ax1.set_ylabel('')
,因此在清洁后,我们可以制作所需的标签,而不是使用sns.heatmap
自动生成的标签.另外,与使用fig.text
手动设置相比,通过这种方式可以更好地生成图中的标签.
I made an adjustment declaring the subplots axis with fig, (ax1, ax2) = plt.subplots(1, 2, sharex=True, sharey=True)
, which is better for understanding the code. I set all axes labels to ""
with the likes of: ax1.set_ylabel('')
, so after cleaning, we can make the labels we want, instead of those auto-generated with sns.heatmap
. Also, the labels in the figure are better generated this way than manually set using fig.text
.
cbar_kws={"orientation": "horizontal"}
是sns.heatmap
的参数,它使颜色条变为水平.
cbar_kws={"orientation": "horizontal"}
is the argument for sns.heatmap
that makes the colorbars horizontal.
使用以下代码:
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()
这将生成此图像:
如果您只希望有一个大的水平颜色条,则可以将代码更改为以下内容:
If you wish to have only one big horizontal colorbar you can change the code to the following:
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
.我希望它每次都能工作,并绘制图像:
We are getting the mappable object: mappable = im.get_children()[0]
and then creating a plt.colorbar
using this mappable object and [ax1,ax2]
as the ax
paramater. I expect this to work every time, it plots the image:
这篇关于一个水平的颜色条,用于Seaborn热图子图和带有xticklabel的Annot问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!