9

I have the following snippet in the first cell of a Jupyter notebook:

import matplotlib.pyplot as plt
import pandas as pd
import ipywidgets as widgets
import numpy as np

out = widgets.Output()
data = pd.DataFrame(np.random.normal(size = 50))
plt.ioff()
with out:
    fig, axes = plt.subplots()
    data.hist(ax = axes)
    display(fig)
plt.ion()    
display(out)

If I restart the kernel and run this first cell, I see this output:

<Figure size 640x480 with 1 Axes>

However, if I run this first cell a second time, I see a matplotlib figure as I intended. This behavior also shows up if I move everything after the import of matplotlib to a second cell, restart the kernel, and rerun the entire notebook.

Is this difference in behavior intentional?

Arthur Azevedo De Amorim
  • 23,012
  • 3
  • 33
  • 39

1 Answers1

4

The code rearranging and adding magic command '%matplotlib notebook' work for me.

%matplotlib notebook
import matplotlib.pyplot as plt
import pandas as pd
import ipywidgets as widgets
import numpy as np

out = widgets.Output()

plt.ioff()
fig, axes = plt.subplots()
plt.ion()

data = pd.DataFrame(np.random.normal(size = 50))
data.hist(ax = axes)

display(out)

with out:
    display(fig)
alz
  • 41
  • 6