22

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!

5
  • 2
    Is this in Jupyter, cause it looks like %matplotlib inline is missing? Commented Oct 17, 2018 at 19:45
  • Yes it is in Jupyter
    – 2Obe
    Commented Oct 17, 2018 at 19:46
  • 2
    ok add "%matplotlib inline" at the top and run again Commented Oct 17, 2018 at 19:47
  • That was the solution. Please post it as an answer
    – 2Obe
    Commented Oct 17, 2018 at 19:47
  • Ok. @2Obe. Added as a solution Commented Oct 17, 2018 at 19:51

1 Answer 1

37

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)

5
  • 1
    For future readers: in Jupyterlab 0.35.4 and sympy.plotting.plot, %matplotlib inline works but the second fancy one doesn't giving me: Javascript Error: IPython is not defined Commented Jan 24, 2019 at 23:12
  • 1
    Mind also that interactive plotting with e.g. animate() is not easily possible with %matplotlib inline. Therefore %matplotlib notebook may be preferred
    – 2Obe
    Commented Mar 29, 2019 at 12:18
  • @RobertLugg the second one worked for me, just now.
    – Shilan
    Commented Jul 6, 2020 at 10:50
  • the first one that worked for me.
    – muhe
    Commented Dec 24, 2021 at 22:04
  • does this work on databricks as well? I tried both but couldnt see the graph. Commented Mar 4, 2022 at 11:54

Not the answer you're looking for? Browse other questions tagged or ask your own question.