python - 如何在单个图中绘制多个seaborn.distplot

标签 python matplotlib seaborn

我想在同一个窗口下绘制多个seaborn distplot,其中每个图都有相同的 x 和 y 网格。我的尝试如下所示,但不起作用。

# function to plot the density curve of the 200 Median Stn. MC-losses
def make_density(stat_list,color, layer_num):

    num_subplots = len(stat_list)
    ncols = 3
    nrows = (num_subplots + ncols - 1) // ncols
    fig, axes = plt.subplots(ncols=ncols, nrows=nrows, figsize=(ncols * 6, nrows * 5))
    
    for i in range(len(stat_list)):
        
        # Plot formatting
        plt.title('Layer ' + layer_num)
        plt.xlabel('Median Stn. MC-Loss')
        plt.ylabel('Density')
        plt.xlim(-0.2,0.05)
        plt.ylim(0, 85)
        min_ylim, max_ylim = plt.ylim()
    
        # Draw the density plot.
        sns.distplot(stat_list, hist = True, kde = True,
                 kde_kws = {'linewidth': 2}, color=color)

# `stat_list` is a list of 6 lists
# I want to draw histogram and density plot of 
# each of these 6 lists contained in `stat_list` in a single window,
# where each row containing the histograms and densities of the 3 plots
# so in my example, there would be 2 rows of 3 columns of plots (2 x 3 =6).
stat_list = [[0.3,0.5,0.7,0.3,0.5],[0.2,0.1,0.9,0.7,0.4],[0.9,0.8,0.7,0.6,0.5]
          [0.2,0.6,0.75,0.87,0.91],[0.2,0.3,0.8,0.9,0.3],[0.2,0.3,0.8,0.87,0.92]]

如何修改函数以在同一窗口下绘制多个 distplot,其中每个显示图的 x 和 y 网格相同?

谢谢,

PS:另外,我希望 6 个分布图具有相同的颜色,最好全部为绿色。

最佳答案

  • 最简单的方法是将数据加载到 pandas 中,然后使用 seaborn.displot .
  • .displot 替换 .distplot在seaborn版本0.11.0中
    • 从技术上讲,您之前想要的是使用 distplot 映射的 FacetGrid
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

# data
stat_list = [[0.3,0.5,0.7,0.3,0.5], [0.2,0.1,0.9,0.7,0.4], [0.9,0.8,0.7,0.6,0.5], [0.2,0.6,0.75,0.87,0.91], [0.2,0.3,0.8,0.9,0.3], [0.2,0.3,0.8,0.87,0.92]]

# load the data into pandas and then transpose it for the correct column data
df = pd.DataFrame(stat_list).T

# name the columns; specify a layer number
df.columns = ['A', 'B', 'C', 'D', 'E', 'F']

# now stack the data into a long (tidy) format
dfl = df.stack().reset_index(level=1).rename(columns={'level_1': 'Layer', 0: 'Median Stn. MC-Loss'})

# plot a displot
g = sns.displot(data=dfl, x='Median Stn. MC-Loss', col='Layer', col_wrap=3, kde=True, color='green')
g.set_axis_labels(y_var='Density')
g.set(xlim=(0, 1.0), ylim=(0, 3.0))

enter image description here

sns.FacetGridsns.distplot

  • .distplot 已弃用
p = sns.FacetGrid(data=dfl, col='Layer', col_wrap=3, height=5)
p.map(sns.distplot, 'Median Stn. MC-Loss', bins=5, kde=True, color='green')
p.set(xlim=(0, 1.0))

关于python - 如何在单个图中绘制多个seaborn.distplot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63924125/

相关文章:

python - Pandas 数据框中的新变量计算连续值

python - 通过 pandas 从 JSON 加载文本值

python - 在 mac osx lion 中安装 MatplotLib

python - 如何将流线从 matplotlib 导入 ArcGIS?

python-3.x - 如何订购seaborn pointplot

python-3.x - sns.pairplot 显示密度曲线而不是直方图

python - 检查字符串是否以子字符串元组开头并在python中获取匹配的字符串

python - pandas to_hdf 函数获取非法指令

python - 如何将图像存储在变量中

python - 未找到 Seaborn 模块 'histplot'