如何并排绘制密度图

2024-05-18 19:14:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在Python上绘制多个密度图,不是作为单个图,而是作为单个窗口下的多个图。如何使用Python的matplotlib实现这一点

下面是我尝试过的,但不起作用:

import numpy as np
import torch
from matplotlib import pyplot as plt
from matplotlib.pyplot import (plot, savefig, xlim, figure,
                              ylim, legend, boxplot, setp,
                              axes, xlabel, ylabel, xticks,
                              axvline)
import seaborn as sns

layer_list_G1_G2 = [[80.,69.,52.], [82.,83.,80.],
                [78.,81.,59.]]

def make_density(layer_list, color, nlayer):

    
    fig = plt.figure(figsize=(20, 6))
    grid = plt.GridSpec(2, 6)

    ax_main = fig.add_subplot(grid[0, 0])
    
    plt.title('Density Plot of Median Stn. MC-Losses at Layers 1 - 12')
    plt.xlabel('MC-Loss')
    plt.ylabel('Density')
    plt.xlim(-0.2,0.05)
    plt.ylim(0, 85)
    min_ylim, max_ylim = plt.ylim()
    
    for j in range(nlayer):
        
        layer_list_tensor = torch.tensor(layer_list[j]) 
        
        den_j = fig.add_subplot(grid[j//6, j % 6], sharex=ax_main, sharey=ax_main)
        
                # Draw the density plot
        den_j.sns.distplot(layer_list, hist = False, kde = True,
                 kde_kws = {'linewidth': 2}, color=color)
  
        plt.axvline(layer_list_tensor.median().tolist(), color='orange', linestyle='dashed', linewidth=1.5)

        plt.text(layer_list_tensor.median().tolist()*0.87, 80, 'median: {:.2f}'.format(layer_list_tensor.median().tolist()))

>>> make_density(layer_list_G1_G2, 'green', 12)

谢谢,


Tags: importlayermatplotlibmainasfigpltax
1条回答
网友
1楼 · 发布于 2024-05-18 19:14:38

您需要将轴引用传递给distplotax=关键字

乙二醇

den_j = fig.add_subplot(grid[j//6, j % 6], sharex=ax_main, sharey=ax_main)
# Draw the density plot
sns.distplot(layer_list, hist = False, kde = True,
                 kde_kws = {'linewidth': 2}, color=color, ax=den_j)
#                                                         ^^^^^^^^

相关问题 更多 >

    热门问题