【问题标题】:Trying to print two heatmaps on the same axis using subplots尝试使用子图在同一轴上打印两个热图
【发布时间】:2020-09-24 11:45:39
【问题描述】:

我正在尝试使用具有相同 y 轴的子图并排打印两个热图。我正在使用 seaborn 绘制它们。

这是我的两个热图的代码。数据是具有 45 列的大型数据框的一部分。我为下面的两个热图分别使用了 4 列。

sns.heatmap(genre_top10.iloc[:,[13,16,19,22]], annot = True, fmt = '.0f', linewidths=.5)
plt.show()
sns.heatmap(genre_top10.iloc[:,[14,17,20,23]], annot = True, fmt = '.0f', linewidths=.5)
plt.show()

但无法弄清楚如何将它们打印为具有共享 y 轴的子图。请帮忙。

【问题讨论】:

  • 在绘图前创建一个图形,其中有 2 个子图和共享 y 轴。然后将轴作为参数传递给 seaborn 热图绘图函数
  • fig, axes = plt.subplots(ncols=2, sharey=True) 然后是 sns.heatmap(...., ax=axes[0])sns.heatmap(...., ax=axes[1])

标签: python matplotlib seaborn heatmap subplot


【解决方案1】:

您可以使用plt.subplots 并将热图分配给不同的轴。我不确定您的数据结构,但这样的事情会将热图添加到不同的轴上。

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

genre_top10 = pd.DataFrame(np.random.rand(4,30))

fig, ax = plt.subplots(ncols=1, nrows=2)
sns.heatmap(genre_top10.iloc[:,[13,16,19,22]], annot = True, fmt = '.0f', linewidths=.5, ax=ax[0])
sns.heatmap(genre_top10.iloc[:,[14,17,20,23]], annot = True, fmt = '.0f', linewidths=.5, ax=ax[1])
plt.show()

【讨论】:

  • 非常感谢@filiphl。这就像一个魅力:)
猜你喜欢
  • 1970-01-01
  • 2018-07-27
  • 1970-01-01
  • 2020-10-12
  • 2020-12-16
  • 2015-04-04
  • 2016-01-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多