3

Unless the user interacts with widget, ipywidgets doesn't show the plot. How can I solve this? Perhaps if I could update a slider value using some code, the plot would show. I think this issue only arises when the Layout configuration method is used.

import numpy as np
from matplotlib import pyplot as plt
from ipywidgets import interactive, fixed, FloatSlider, HBox, Layout
from IPython.display import display

ts = np.arange(0.0, 5.0, 0.01)

def plotFunc(omega):
    plt.plot(ts, np.cos(omega*ts))

w = interactive(plotFunc, omega = FloatSlider(min = 0, max = 12, step = 1, value = 6, orientation = 'vertical'))
box_layout = Layout(display = 'flex', flex_flow = 'row', justify_content = 'center', align_items = 'center')
display(HBox([w.children[-1], w.children[0]], layout = box_layout))
Miladiouss
  • 4,270
  • 1
  • 27
  • 34

1 Answers1

3

To enable the inline backend for usage with the IPython Notebook:

%matplotlib inline

and this at the end of your code:

w.update()
There is some explanation here:

%matplotlib inline commands IPython Documentation,

also IPython.display.set_matplotlib_formats and IPython.display.set_matplotlib_close for more information on changing additional behaviors of the inline backend.

Alex F
  • 756
  • 2
  • 9
  • 24
  • It works thanks. I just don't know why `%matplotlib inline` is necessary. Without it, it wouldn't work though. – Miladiouss Feb 07 '19 at 22:31