Capture Cell Magic
- cell magic for output capture:
%%capture
: captures output and stderr streams
store streams in variable or discard streams (default)%%capture myCapture print('this is standard output') print('this is stderr', file=sys.stderr) myCapture() # prints captured output myCapture.stdout # prints stdout string myCapture.stderr # prints stderr string
Plotting
- Import
numpy
andmatplotlib
import numpy as np import matplotlib.pyplot # use capture cell magic to capture plot %%capture myVerbosePlot print("Building x-axis data points...") # 300 points spaced evenly between 0 and 5 x = np.linspace(0, 5, 300) print("Building y-axis data points...") y = np.cos(x) print("Performing plot and labeling axes") plt.plt(x,y) plt.xlabel('x') plt.ylabel('cos(x)') print("Displaying Plot") plt.show() # then use myVerbosePlot() in the next cell to display output and plot