使用子图时,如何在jupyter中停止两次打印图? [英] How to stop plots printing twice in jupyter when using subplots?

查看:206
本文介绍了使用子图时,如何在jupyter中停止两次打印图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理泰坦尼克号数据,并且尝试使用pyplot和seaborn的组合来生成一些子图. 我编写了以下代码,以在3x2网格中创建6个子图;

I'm working with the titanic data and I'm trying to use a combination of pyplot and seaborn to produce some subplots. I've written the following code to create 6 subplots in a 3x2 grid;

plt.rcParams['figure.figsize'] = [12, 8]
fig, axes = plt.subplots(nrows=3, ncols=2)
plt.tight_layout()

_ = sns.catplot(x='Pclass', y='Age', data=train_df, kind='box', height=8, palette=col_pal, ax=axes[0, 0])
_ = sns.catplot(x='Embarked', y='Age', data=train_df, kind='box', height=8, palette=col_pal, ax=axes[0, 1])
_ = sns.catplot(x='Sex', y='Age', data=train_df, kind='box', height=8, palette=col_pal, ax=axes[1, 0])
_ = sns.catplot(x='Sex', y='Age', hue='Pclass', data=train_df, kind='box', height=8, palette=col_pal, ax=axes[1, 1])
_ = sns.catplot(x='SibSp', y='Age', data=train_df, kind='box', height=8, palette=col_pal, ax=axes[2, 0])
_ = sns.catplot(x='Parch', y='Age', data=train_df, kind='box', height=8, palette=col_pal, ax=axes[2, 1])
plt.show()

当我在笔记本中运行此程序时,它会成功创建所需的图,但是,此后它还会打印出6个空白图.

When I run this in my notebook, it succesfully creates the desired plot, however, it also prints out 6 blank plots afterwards.

如何抑制这些空图打印到输出中?

How can I suppress these empty plots from printing into my output?

推荐答案

将每个图分配给像g这样的变量,然后使用plt.close(g.fig)删除不需要的子图.或遍历所有sns.axisgrid.FacetGrid类型变量并按如下所示关闭它们:

Assign each of your plots to a variable like g, and use plt.close(g.fig) to remove your unwanted subplots. Or iterate over all sns.axisgrid.FacetGrid type variables and close them like so:

for p in plots_names:
    plt.close(vars()[p].fig)

下面的完整代码段就可以做到这一点.请注意,我正在使用train_df = sns.load_dataset("titanic")加载泰坦尼克号数据集.在这里,所有列名都是小写字母,与您的示例不同.我还删除了palette=col_pal参数,因为您的代码段中未定义col_pal.

The complete snippet below does just that. Note that I'm loading the titanic dataset using train_df = sns.load_dataset("titanic"). Here, all column names are lower case unlike in your example. I've also removed the palette=col_pal argument since col_pal is not defined in your snippet.

情节:

代码:

import seaborn as sns
import matplotlib.pyplot as plt

plt.rcParams['figure.figsize'] = [12, 8]
fig, axes = plt.subplots(nrows=3, ncols=2)
plt.tight_layout()

train_df = sns.load_dataset("titanic")

g = sns.catplot(x='pclass', y='age', data=train_df, kind='box', height=8, ax=axes[0, 0])
h = sns.catplot(x='embarked', y='age', data=train_df, kind='box', height=8, ax=axes[0, 1])
i = sns.catplot(x='sex', y='age', data=train_df, kind='box', height=8, ax=axes[1, 0])
j = sns.catplot(x='sex', y='age', hue='pclass', data=train_df, kind='box', height=8, ax=axes[1, 1])
k = sns.catplot(x='sibsp', y='age', data=train_df, kind='box', height=8, ax=axes[2, 0])
l = sns.catplot(x='parch', y='age', data=train_df, kind='box', height=8, ax=axes[2, 1])

# iterate over plots and run
# plt.close() to prevent duplicate
# subplot setup
var_dict = vars().copy()
var_keys = var_dict.keys()
plots_names = [x for x in var_keys if isinstance(var_dict[x], sns.axisgrid.FacetGrid)]
for p in plots_names:
    plt.close(vars()[p].fig)

请注意,您 必须将图分配给变量名 ,此功能才能起作用.如果仅添加将图关闭的代码段到原始代码段的末尾,则重复的子图设置将保持不变.

Please note that you will have to assign your plots to variable names for this to work. If you just add the snippet that closes the plots to the end of your original snippet, the duplicate subplot setup will remain untouched.

代码2:

import seaborn as sns
import matplotlib.pyplot as plt

plt.rcParams['figure.figsize'] = [12, 8]
fig, axes = plt.subplots(nrows=3, ncols=2)
plt.tight_layout()

train_df = sns.load_dataset("titanic")

_ = sns.catplot(x='pclass', y='age', data=train_df, kind='box', height=8, ax=axes[0, 0])
_ = sns.catplot(x='embarked', y='age', data=train_df, kind='box', height=8, ax=axes[0, 1])
_ = sns.catplot(x='sex', y='age', data=train_df, kind='box', height=8, ax=axes[1, 0])
_ = sns.catplot(x='sex', y='age', hue='pclass', data=train_df, kind='box', height=8, ax=axes[1, 1])
_ = sns.catplot(x='sibsp', y='age', data=train_df, kind='box', height=8, ax=axes[2, 0])
_ = sns.catplot(x='parch', y='age', data=train_df, kind='box', height=8, ax=axes[2, 1])

# iterate over plots and run
# plt.close() to prevent duplicate
# subplot setup
var_dict = vars().copy()
var_keys = var_dict.keys()
plots_names = [x for x in var_keys if isinstance(var_dict[x], sns.axisgrid.FacetGrid)]
for p in plots_names:
    plt.close(vars()[p].fig)

图2:

这篇关于使用子图时,如何在jupyter中停止两次打印图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆