Based on this working code, I'd suggest you assign your plot to a variable, my_plot
for instance, and then try plt.show(my_plot)
or display(my_plot.figure)
.
I outlined the approach using a donut plot in a notebook here. You need to run the notebook though to see it rendered. To do that go to here, and press launch binder
next to the text 'Start with the donut plot on a tab widget demo as a notebook'. In the process outlined, I had already invoked the plot making earlier in the notebook. And so I mention a couple of ways to add it to the tab above.
Summarizing from there:
With a donut plot defined as donut_plot
earlier in a notebook, the following code adds it to the first tab on a simple tab system:
%matplotlib inline
# based on https://stackoverflow.com/a/51060721/8508004
# and https://github.com/jupyter-widgets/ipywidgets/issues/1754
# combined with donut plot from
# https://github.com/fomightez/donut_plots_with_subgroups/blob/master/demo_basics_from_df.ipynb
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)
display(donut_plot.figure)
with out2:
fig2, axes2 = plt.subplots()
data2.hist(ax = axes2)
plt.show(fig2)
UPDATING WITH MORE SELF-CONTAINED EXAMPLE WITH PIE PLOT BELOW:
In response to OP expressing trouble with adding in their pie plot, I added another example in the notebook I linked above. It boils down to below. To make the code block more self-contained than above, I included the dataframe used as well:
%matplotlib inline
# based on https://stackoverflow.com/a/51060721/8508004
# and https://github.com/jupyter-widgets/ipywidgets/issues/1754
# combined with donut plot from
# https://github.com/fomightez/donut_plots_with_subgroups/blob/master/demo_basics_from_df.ipynb
import pandas as pd
obs = [('A', 1, "frizzled"),
('A', 1, "lethargic"),
('A', 1, "polythene"),
('A', 1, "epic"),
('A', 2, "frizzled"),
('A', 2, "lethargic"),
('A', 2, "epic"),
('A', 3, "frizzled"),
('A', 3, "lethargic"),
('A', 3, "polythene"),
('A', 3, "epic"),
('A', 3, "bedraggled"),
('B', 1, "frizzled"),
('B', 1, "lethargic"),
('B', 1, "polythene"),
('B', 1, "epic"),
('B', 1, "bedraggled"),
('B', 1, "moombahcored"),
('B', 2, "frizzled"),
('B', 2, "lethargic"),
('B', 2, "polythene"),
('B', 2, "epic"),
('B', 2, "bedraggled"),
('C', 1, "frizzled"),
('C', 1, "lethargic"),
('C', 1, "polythene"),
('C', 1, "epic"),
('C', 1, "bedraggled"),
('C', 1, "moombahcored"),
('C', 1, "zoned"),
('C', 1, "erstaz"),
('C', 1, "mined"),
('C', 1, "liberated"),
('C', 2, "frizzled"),
('C', 2, "lethargic"),
('C', 2, "polythene"),
('C', 2, "epic"),
('C', 2, "bedraggled"),
('C', 3, "frizzled"),
('C', 3, "lethargic"),
('C', 3, "polythene"),
('C', 3, "epic"),
('C', 3, "bedraggled"),
('C', 4, "bedraggled"),
('C', 4, "frizzled"),
('C', 4, "lethargic"),
('C', 4, "polythene"),
('C', 4, "epic"),
('C', 5, "frizzled"),
('C', 5, "lethargic"),
('C', 5, "polythene"),
('C', 5, "epic"),
('C', 5, "bedraggled"),
('C', 5, "moombahcored")]
labels = ['group', 'subgroup', 'sub-subgroup']
df = pd.DataFrame.from_records(obs, columns=labels)
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)
grouped = df.groupby("group")
grouped.size()
group_names= grouped.size().index.tolist()
group_size= grouped.size().tolist()
my_plot = plt.pie(group_size, labels=group_names,autopct="%0.f%%",radius=2.4)
plt.show(my_plot)
with out2:
fig2, axes2 = plt.subplots()
data2.hist(ax = axes2)
plt.show(fig2)
Update:
What if you had more than a few plots to put onto tabs? You may want to make a loop that can handle this without needing to code the with
blocks for each by hand.
While I haven't test it yet, I ran across this suggestion.