简体   繁体   English

如何并排绘制密度图

[英]How to plot density plots side-by-side

I would like to plot multiple density plots on Python, not as a single plot, but as multiple plots under a single window.我想在 Python 上绘制多个密度图,而不是作为单个图,而是作为单个窗口下的多个图。 How can I do this with matplotlib of Python?如何使用 Python 的matplotlib执行此操作?

Below is what I tried, which doesn't work:以下是我尝试过的,但不起作用:

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)

Thank you,谢谢,

You need to pass the axes reference to the ax= keyword of distplot您需要将轴引用传递给distplotax=关键字

eg例如

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)
#                                                         ^^^^^^^^

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM