Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ipywidgets doesn't show plot before interaction

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))
like image 468
Miladiouss Avatar asked Oct 28 '25 18:10

Miladiouss


1 Answers

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.

like image 167
Alex F Avatar answered Oct 30 '25 15:10

Alex F