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

30,580

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)

Share:
30,580
2Obe
Author by

2Obe

Standing at the mystic data science forest's edge - watching the deep green trees staggering gently in the warm wind and smelling the sweet odor of an big adventure - I am truly grateful

Updated on July 05, 2022

Comments

  • 2Obe
    2Obe almost 2 years

    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!