rrk*_*rkk 5 python heatmap python-3.x seaborn
我试图在一个图中编写多个热图。我写了下面的代码,我有两个问题。
(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')
Run Code Online (Sandbox Code Playgroud)
(1)我想要每个单元格中的数据值,我不需要每张图片的轴标签。因此,我设置了 xticklabels、yticklables 和 annot;但它们并没有反映在图中。我应该怎么做?
这是最近修复的一个问题,whenxticklabels = False或yticklabels = False,annot = True不起作用。解决方法是将xticklabels和yticklabels都设置为空字符串列表[""]。
我做了一个调整,用 声明子图轴fig, (ax1, ax2) = plt.subplots(1, 2, sharex=True, sharey=True),这更好地理解代码。我将所有轴标签设置""为:ax1.set_ylabel(''),因此在清洁后,我们可以制作我们想要的标签,而不是使用sns.heatmap. 此外,图中的标签以这种方式生成比使用手动设置更好fig.text。
(2) 我可以旋转颜色条吗?
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()
Run Code Online (Sandbox Code Playgroud)
这将生成此图像:
如果您希望只有一个大的水平颜色条,您可以将代码更改为以下内容:
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()
Run Code Online (Sandbox Code Playgroud)
我们正在获取可映射对象:mappable = im.get_children()[0]然后plt.colorbar使用此可映射对象并[ax1,ax2]作为ax参数创建一个。我希望这每次都能工作,它绘制图像:
| 归档时间: |
|
| 查看次数: |
6100 次 |
| 最近记录: |