How to display matplotlib plots in a Jupyter tab widget? How to display matplotlib plots in a Jupyter tab widget? python python

How to display matplotlib plots in a Jupyter tab widget?


I added a couple of things to make your code work as you would like

  • Add %matplotlib inline at the top of the cell
  • Replace your display(fig) calls with plt.show(fig) calls.
%matplotlib inlineimport matplotlib.pyplot as pltimport pandas as pdimport ipywidgets as widgetsimport numpy as npout1 = widgets.Output()out2 = widgets.Output()data1 = pd.DataFrame(np.random.normal(size = 50))data2 = pd.DataFrame(np.random.normal(size = 100))tab = widgets.Tab(children = [out1, out2])tab.set_title(0, 'First')tab.set_title(1, 'Second')display(tab)with out1:    fig1, axes1 = plt.subplots()    data1.hist(ax = axes1)    plt.show(fig1)with out2:    fig2, axes2 = plt.subplots()    data2.hist(ax = axes2)    plt.show(fig2)


plt.show(fig) in the answer above from ac24 is now deprecated:

In [1]: import matplotlib.pyplot as plt                                                                                                                                   In [2]: fig = plt.figure()                                                                                                                                                In [3]: plt.show(fig)                                                                                                                                                     <ipython-input-3-d1fd62acb551>:1: MatplotlibDeprecationWarning: Passing the block parameter of show() positionally is deprecated since Matplotlib 3.1; the parameter will become keyword-only in 3.3.    plt.show(fig)

plt.show(block=True) (or plt.show(block=False)) is the keyword-only call.