【问题标题】:seaborn stop figure from being visualizedseaborn 阻止图形可视化
【发布时间】:2023-02-02 21:19:43
【问题描述】:

使用 seaborn 生成的图形正在可视化,即使没有 f.show()。

我希望这个数字只在我调用它时才可视化。

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

def plot_kde_x (x):
    sns.set(style="ticks")
    
    f, (ax_box, ax_hist) = plt.subplots(2, sharex=True, 
                                        gridspec_kw={"height_ratios": (.15, .85)})

    sns.boxplot(x, ax=ax_box)
    sns.kdeplot(x, ax=ax_hist)

    ax_box.set(yticks=[])
    sns.despine(ax=ax_hist)
    sns.despine(ax=ax_box, left=True)
    return f 


x = np.random.randint(1,10,100)
# figure should not be displayed
f = plot_kde_x(x)

OUT,图形仍然显示

【问题讨论】:

    标签: python seaborn


    【解决方案1】:

    如果您使用的是 jupyter,该图会显示两次,因为 plot_kde_x(x) 返回了该图。

    删除返回,或者,如果您仍然希望能够捕获图形对象,请在函数后添加 ;

    plot_kde_x(x);
    

    或者分配给一个变量:

    f = plot_kde_x(x)
    

    【讨论】:

    • 谢谢,有没有办法停止第一个显示(在创建时)?如果我做 f = plot_kde_x(x),我希望它显示 0 次,就像我使用 plotly 一样
    • 为了清楚起见,我进行了编辑,提出问题“除非 if.show() 被调用,否则永远不要想象这个数字
    猜你喜欢
    • 1970-01-01
    • 2015-02-03
    • 2017-02-02
    • 1970-01-01
    • 1970-01-01
    • 2020-09-08
    • 1970-01-01
    • 1970-01-01
    • 2018-03-20
    相关资源
    最近更新 更多