简体   繁体   English

seaborn distplot 循环懒惰评估

[英]seaborn distplot loop lazy evaluation

I am using ipython notebook and trying to use the following function to export seaborn distplots.我正在使用 ipython notebook 并尝试使用以下函数导出 seaborn distplots。 It works just fine if I call the function and execute with only one variable at a time.如果我调用函数并一次只执行一个变量,它就可以正常工作。 If I call the function in a loop, it continues to build on top of the distplot from the previous function call.如果我在循环中调用该函数,它会继续在前一个函数调用的 distplot 之上构建。

My desired output would be for the function to output a new displot every time it is called in a loop.我想要的输出是函数每次在循环中调用时都输出一个新的 displot。 Is there a way to force evaluation or a new distplot?有没有办法强制评估或新的 distplot?

def graph_extraversion (x):


    file_name = "extraversion_" + str(x) + ".png"
    sns_plot = sns.distplot(Personalities[Personalities.labels1 ==x].extraversion)
    sns_plot = sns.distplot(df.extraversion)
    fig = sns_plot.get_figure()
    fig.savefig(file_name)
    new_stat = Personalities[Personalities.labels1 ==x].extraversion.describe()
    extraversion_drift = extraversion_median - new_stat[1]
    drift = extraversion_drift / extraversion_std
    if (drift >= 1) | (drift <= -1):
        return "1 std deviation or more"
    else:
        return "Less than one std deviation"

This what is what the distplot looks like after one call这就是一次调用后 distplot 的样子

在此处输入图片说明

This is two calls later in a loop.这是循环中稍后的两次调用。

在此处输入图片说明

Again this works just fine with a single call and execution but when looped it keeps building.同样,这在一次调用和执行中工作得很好,但在循环时它会继续构建。

So this has to do with matplotlib and closing figures.所以这与 matplotlib 和结束数字有关。

additional code required is an import:所需的附加代码是导入:

import matplotlib.pyplot as plt

Then at the end of the func:然后在函数的末尾:

plt.close(fig)

This should help with any looping with both seaborn and matplotlib这应该有助于使用 seaborn 和 matplotlib 进行任何循环

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM