簡體   English   中英

在Matplotlib中並排繪制兩張熱圖

[英]Plotting two heat maps side by side in Matplotlib

我有一個函數,用於繪制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運行時輸出如下內容:

在此輸入圖像描述

我想要做的是並排繪制這些熱圖中的兩個,但我在這方面遇到了一些麻煩。 到目前為止我所做的是嘗試將每個熱圖分配給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運行時)輸出以下圖像:

在此輸入圖像描述

你需要的是plt.subplots函數。 您可以將Figure與多個Axes初始化,而不是手動將Axes對象添加到Figure 然后, matshow在每個Axes上調用matshow一樣簡單:

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)

在此輸入圖像描述

您可以稍后執行所有其他格式設置。

請按照以下示例,將繪圖更改為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() 

輸出:

產量

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM