在Matplotlib中并排绘制两个热图

时间:2019-04-13 07:17:54

标签: python matplotlib subplot

我有一个函数,可以为DataFrame的相关矩阵绘制热图。该函数如下所示:

def corr_heatmap(data):
    columns = data.columns
    corr_matrix = data.corr()

    fig, ax = plt.subplots(figsize=(7, 7))
    mat = ax.matshow(corr_matrix, cmap='coolwarm')

    ax.set_xticks(range(len(columns)))
    ax.set_yticks(range(len(columns)))
    ax.set_xticklabels(columns)
    ax.set_yticklabels(columns)
    plt.setp(ax.get_xticklabels(), rotation=45, ha='left', rotation_mode='anchor')
    plt.colorbar(mat, fraction=0.045, pad=0.05)
    fig.tight_layout()
    plt.show()

    return mat

,当与DataFrame一起运行时,会输出以下内容:

enter image description here

我想做的是并排绘制两个热图,但是这样做有些麻烦。到目前为止,我所做的是尝试将每个热图分配给AxesImage对象,并使用子图对其进行绘制。

mat1 = corr_heatmap(corr_mat1)
mat2 = corr_heatmap(corr_mat2)

fig = plt.figure(figsize=(15, 15))
ax1 = fig.add_subplot(221)
ax2 = fig.add_subplot(222)
ax1.plot(ma1)
ax2.plot(ma2)

但这给了我以下错误:

TypeError: float() argument must be a string or a number, not 'AxesImage'

有人会碰巧知道我可以并排绘制两个热图图像的方法吗?谢谢。

编辑

万一有人想知道我想要做的最终代码是什么样的:

def corr_heatmaps(data1, data2, method='pearson'):

    # Basic Configuration
    fig, axes = plt.subplots(ncols=2, figsize=(12, 12))
    ax1, ax2 = axes
    corr_matrix1 = data1.corr(method=method)
    corr_matrix2 = data2.corr(method=method)
    columns1 = corr_matrix1.columns
    columns2 = corr_matrix2.columns

    # Heat maps.
    im1 = ax1.matshow(corr_matrix1, cmap='coolwarm')
    im2 = ax2.matshow(corr_matrix2, cmap='coolwarm')

    # Formatting for heat map 1.
    ax1.set_xticks(range(len(columns1)))
    ax1.set_yticks(range(len(columns1)))
    ax1.set_xticklabels(columns1)
    ax1.set_yticklabels(columns1)
    ax1.set_title(data1.name, y=-0.1)
    plt.setp(ax1.get_xticklabels(), rotation=45, ha='left', rotation_mode='anchor')
    plt.colorbar(im1, fraction=0.045, pad=0.05, ax=ax1)

    # Formatting for heat map 2.
    ax2.set_xticks(range(len(columns2)))
    ax2.set_yticks(range(len(columns2)))
    ax2.set_xticklabels(columns2)
    ax2.set_yticklabels(columns2)
    ax2.set_title(data2.name, y=-0.1)
    plt.setp(ax2.get_xticklabels(), rotation=45, ha='left', rotation_mode='anchor')
    plt.colorbar(im2, fraction=0.045, pad=0.05, ax=ax2)

    fig.tight_layout()

这可能(当与两个Pandas DataFrames一起运行时)沿着以下图像输出内容:

enter image description here

2 个答案:

答案 0 :(得分:2)

您需要的是plt.subplots函数。您可以将Axes连同许多Figure一起初始化,而不是手动将Figure对象添加到Axes。然后,就像在每个matshow上调用Axes一样简单:

import numpy as np
import pandas as pd

from matplotlib import pyplot as plt

df = pd.DataFrame(np.random.rand(10, 10))

fig, axes = plt.subplots(ncols=2, figsize=(8, 4))

ax1, ax2 = axes

im1 = ax1.matshow(df.corr())
im2 = ax2.matshow(df.corr())

fig.colorbar(im1, ax=ax1)
fig.colorbar(im2, ax=ax2)

enter image description here

您以后可以执行所有其他格式化。

答案 1 :(得分:-1)

请按照以下示例,将绘图更改为matshow,根据需要进行轴自定义。

import numpy as np 
import matplotlib.pyplot as plt 

def f(t): 
    return np.exp(-t) * np.cos(2*np.pi*t) 

t1 = np.arange(0.0, 3.0, 0.01) 

ax1 = plt.subplot(121) 
ax1.plot(t1, f(t1), 'k') 

ax2 = plt.subplot(122) 
ax2.plot(t1, f(t1), 'r') 
plt.show() 

输出:

Output

相关问题