Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jupyter Notebook - Plot inside a function - Figure is not plotted [duplicate]

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!

like image 304
2Obe Avatar asked Oct 17 '18 19:10

2Obe


1 Answers

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

%matplotlib inline

or

%matplotlib notebook

(for a slightly more fancy plotting option)

like image 137
Christian Sloper Avatar answered Nov 03 '22 09:11

Christian Sloper