Reputation: 45
Imagine I have 10 figures that I would like to show in Jupyter notebook. Using ipywidgets, how can create 10 tabs in a loop? Below I share a sample and if I think of 10 figures, then I would like to create tabs in a loop instead of manual work. Thanks in advance ! Best, DS
%matplotlib inline
import matplotlib.pyplot as plt
import pandas as pd
import ipywidgets as widgets
import numpy as np
out1 = 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)
Upvotes: 2
Views: 3514
Reputation: 296
OK so here is something i came up with, although you will have to provide data out side of the loop, but this should be help full.
%matplotlib inline
import matplotlib.pyplot as plt
import pandas as pd
import ipywidgets as widgets
import numpy as np
data1 = pd.DataFrame(np.random.normal(size = 50))
data2 = pd.DataFrame(np.random.normal(size = 100))
data =[data1,data2]
sub_tab=[widgets.Output() for i in range(len(data))]
tab = widgets.Tab(sub_tab)
for i in range (len(data)):
tab.set_title(i,"Tab {}".format(i+1))
with sub_tab[i]:
fig, axes = plt.subplots()
data[i].hist(ax = axes)
plt.show(fig)
display(tab)
Upvotes: 3