【发布时间】:2021-01-03 12:37:25
【问题描述】:
我想在同一个窗口下绘制多个 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 个分布图具有相同的颜色,最好都是绿色。
【问题讨论】:
-
这在your previous question 看来非常强烈,但你没有使用
for ax in axes,而且你也没有使用sns.distplot(...., ax=ax)。您可以使用plt.subplots(...., sharex=True, sharey=True)共享 x 和 y 轴。您的示例代码似乎只使用了一种颜色。注意plt.xlim等需要放在创建主图之后,而不是之前。当只有一个子图时,plt.xlim等效于ax.set_xlim。
标签: python matplotlib seaborn