本文介绍了如何在单个图形中绘制多个seaborn.distplot的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在同一个窗口下绘制多个 seaborn distplot
,其中每个图都有相同的 x 和 y 网格.我的尝试如下所示,但不起作用.
# 绘制 200 Median Stn 密度曲线的函数.MC-损失def make_density(stat_list,color, layer_num):num_subplots = len(stat_list)ncols = 3nrows =(num_subplots + ncols-1)//ncols无花果,轴= plt.subplots(ncols = ncols,nrows = nrows,figsize =(ncols * 6,nrows * 5))对于我在范围内(len(stat_list)):#绘图格式plt.title('图层' + layer_num)plt.xlabel('中位数Stn.MC-Loss')plt.ylabel('密度')plt.xlim(-0.2,0.05)plt.ylim(0,85)min_ylim, max_ylim = plt.ylim()# 绘制密度图.sns.distplot(stat_list, hist = True, kde = True,kde_kws = {'linewidth':2},color = color)# `stat_list` 是一个包含 6 个列表的列表#我要绘制直方图和密度图# 在单个窗口中包含在 `stat_list` 中的这 6 个列表中的每一个,# 其中每一行包含 3 个图的直方图和密度#因此,在我的示例中,将有2行3列的图(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 个 distplots 具有相同的颜色,最好都是绿色.
解决方案
- 最简单的方法是将数据加载到pandas中,然后使用
sns.FacetGrid
和sns.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))
I want to plot multiple seaborn
distplot
under a same window, where each plot has the same x and y grid. My attempt is shown below, which does not work.# 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]]
How can I modify my function to draw multiple
distplot
under the same window, where the x and y grid for each displayed plot is identical?Thank you,
PS: Aside, I want the 6 distplots to have identical color, preferably green for all of them.
解决方案- The easiest method is to load the data into pandas and then use
seaborn.displot
. .displot
replaces.distplot
in seaborn version 0.11.0- Technically, what you would have wanted before, is a
FacetGrid
mapped withdistplot
.
- Technically, what you would have wanted before, is a
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))
sns.FacetGrid
andsns.distplot
.distplot
is deprecated
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))
这篇关于如何在单个图形中绘制多个seaborn.distplot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!