pandas Jupyter Notebook - 在函数内部绘图 - 未绘制图形

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/52862234/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 06:05:29  来源:igfitidea点击:

Jupyter Notebook - Plot inside a function - Figure is not plotted

pythonpython-3.xpandasmatplotlibjupyter-notebook

提问by 2Obe

I want to call a function from a class in which I want to plot several figures. There is no error thrown but I did not receive the plot but only:

我想从一个类中调用一个函数,我想在其中绘制多个数字。没有抛出错误,但我没有收到情节,但只有:

#############################################
Histograms of the continuous data:
#############################################
<Figure size 640x480 with 1 Axes>
<Figure size 640x480 with 1 Axes>
<Figure size 640x480 with 1 Axes>
<Figure size 640x480 with 1 Axes>
<Figure size 640x480 with 1 Axes>
<Figure size 640x480 with 1 Axes>
<Figure size 640x480 with 1 Axes>
<Figure size 640x480 with 1 Axes>
<Figure size 640x480 with 1 Axes>
<Figure size 640x480 with 1 Axes>
<Figure size 640x480 with 1 Axes>
<Figure size 640x480 with 1 Axes>

The code I use is:

我使用的代码是:

class Pipeline:
    import matplotlib.pyplot as plt
    global plt
    from matplotlib import style
    style.use('ggplot')  


    def __init__(self,goal):
        self.goal = goal


    def examine(self,dataset):
        # Check for categorical and continous data
        continuous = []
        categorical = []
        for n,i in enumerate(dataset.columns):
            if isinstance(dataset[i][1],str):
                categorical.append(dataset.columns[n])
            else:
                continuous.append(dataset.columns[n])

        continuous_data = dataset[continuous]
        categorical_data = dataset[categorical]

        #Plot the histograms of the continuous data
        print('#############################################')
        print('Histograms of the continuous data:')
        print('#############################################')

        for col in continuous_data.columns:
            fig = plt.figure()
            ax = continuous_data[col].hist()
            ax.set_title(col)
            plt.show()







pipe = Pipeline('C')
pipe.examine(data)

I wonder because if I run the same code a second time it plots the figures just as proposed. Appreciate any help!

我想知道,因为如果我第二次运行相同的代码,它会按照建议绘制数字。感谢任何帮助!

回答by Christian Sloper

It looks like you are using Jupyter. To have plots show in Jupyter you can add either

看起来您正在使用 Jupyter。要在 Jupyter 中显示图,您可以添加

%matplotlib inline

or

或者

%matplotlib notebook

(for a slightly more fancy plotting option)

(对于稍微更花哨的绘图选项)