If I had to guess on where you want the ‘following cell’ break, it would be like this with display(dashboard)
being around the end of the first cell perhaps.
So assuming that and building on my answer above we have this option…
Cell #1
Try this in the first cell:
from datetime import datetime
from IPython.display import display
import ipywidgets as widgets
from pandas import DataFrame
from math import sin, pi
import matplotlib.pyplot as plt
# DataFrame to plot
time = [t for t in range(200)]
df = DataFrame( {'time':time, 'data':[sin(4*pi*t/200) for t in time]} )
# Plot the dataframe
df.plot('time', 'data')
# make a matplotlib figure from this
my_plot_as_matplotlib_figure_object = plt.gcf()
# 3 Output widgets in a Tab
dfInput = widgets.Output()
dfOutput = widgets.Output()
Plot2DOutput = widgets.Output()
outputTab = widgets.Tab( [ dfInput, dfOutput, Plot2DOutput ] )
outputTab.set_title( 0, 'Input' )
outputTab.set_title( 1, 'Output' )
outputTab.set_title( 2, '2D Plot' )
# Controls for UI in HBox
cbox = widgets.Combobox( placeholder='method',
options=['Data','Simplex','SMap','CCM'],
description='method:', ensure_option=True,
disabled=False )
controls = widgets.HBox( [ cbox, widgets.IntSlider() ] )
# dashboard UI in VBox : contains controls & outputTab
dashboard = widgets.VBox( [controls, outputTab] )
display(dashboard)
plt.close() # suppress any open active plot object from showing up in cell output for this cell by closing any
Running that as the first cell makes the tabs with no content yet.
By running the next cell (cell #2), we can add the content desired after-the-fact i.e., after the tabs widgets already are generated and displayed.
Cell #2
With everything set up in the first cell, we can use the context managers now to send the print()
and display of the matplotlib figure made from the Pandas plot object to the specified tab:
# Print in dfInput Tab
with dfInput:
print( f'{datetime.now()}' )
print( df )
# Display the plot figure on the Plot2DOutput Tab
with Plot2DOutput:
display(my_plot_as_matplotlib_figure_object)
Note that to get this to work then with everything not in the same cell, I had to add a few more controls beyond what I did above.
- you’ll see I add
plt.close()
to the end of the first cell to close all active plots so Jupyter doesn’t detect an active plot and display it in the output area of the cell. (This is like the extra one you may have been seeing).
- use
plt.gcf()
to make a assigned figure for the Pandas plot (Note that I didn’t need to assign a handle for the plot/axes object here, i.e., no my_plot
, because we need a matplotlib figure object for display()
.)
- display of the matplotlib figure object in the context of
Plot2DOutput
(Matplotlib figures work with display()
. I’ve found these can be reused in other cells and so are handier, see here for more details.)
Alternative implementation with Matpotlib directly (not relying on Pandas plotting)
If I had to guess on where you want the ‘following cell’ break, it would be like this with display(dashboard)
being around the end of the first cell perhaps.
So assuming that and building on my answer above we have this option…
Cell #1
Try this in the first cell:
from datetime import datetime
from IPython.display import display
import ipywidgets as widgets
from pandas import DataFrame
from math import sin, pi
import matplotlib.pyplot as plt
# DataFrame to plot
time = [t for t in range(200)]
df = DataFrame( {'time':time, 'data':[sin(4*pi*t/200) for t in time]} )
# Plot the dataframe
plt.plot(df['time'], df['data'])
# make a matplotlib figure from this
my_plot_as_matplotlib_figure_object = plt.gcf()
# 3 Output widgets in a Tab
dfInput = widgets.Output()
dfOutput = widgets.Output()
Plot2DOutput = widgets.Output()
outputTab = widgets.Tab( [ dfInput, dfOutput, Plot2DOutput ] )
outputTab.set_title( 0, 'Input' )
outputTab.set_title( 1, 'Output' )
outputTab.set_title( 2, '2D Plot' )
# Controls for UI in HBox
cbox = widgets.Combobox( placeholder='method',
options=['Data','Simplex','SMap','CCM'],
description='method:', ensure_option=True,
disabled=False )
controls = widgets.HBox( [ cbox, widgets.IntSlider() ] )
# dashboard UI in VBox : contains controls & outputTab
dashboard = widgets.VBox( [controls, outputTab] )
display(dashboard)
plt.close() # suppress any open active plot object from showing up in cell output for this cell by closing any
Running that as the first cell makes the tabs with no content yet.
By running the next cell (cell #2), we can add the content desired after-the-fact i.e., after the tabs widgets already are generated and displayed.
Cell #2
With everything set up in the first cell, we can use the context managers now to send the print()
and display of the matplotlib figure object to the specified tab:
# Print in dfInput Tab
with dfInput:
print( f'{datetime.now()}' )
print( df )
# Display the plot figure on the Plot2DOutput Tab
with Plot2DOutput:
display(my_plot_as_matplotlib_figure_object)
Note that to get this to work then with everything not in the same cell, I had to add a few more controls beyond what I did above.
- you’ll see I add
plt.close()
to the end of the first cell to close all active plots so Jupyter doesn’t detect an active plot and display it in the output area of the cell. (This is like the extra one you may have been seeing).
- use
plt.gcf()
to make a assigned figure for the plot (Note that I didn’t need to assign a handle for the plot/axes object here, i.e., no my_plot
, because we need a matplotlib figure object for display()
.)
- display of the matplotlib figure object in the context of
Plot2DOutput
(Matplotlib figures work with display()
. I’ve found these can be reused in other cells and so are handier, see here for more details.)