Skip to content

Instantly share code, notes, and snippets.

@jarhoads
Created December 19, 2018 15:46
Show Gist options
  • Save jarhoads/f6bbc8dffc2eaecbe518b7b79dd16d65 to your computer and use it in GitHub Desktop.
Save jarhoads/f6bbc8dffc2eaecbe518b7b79dd16d65 to your computer and use it in GitHub Desktop.
Jupyter and iPython Notes

Jupyter and IPython

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 and matplotlib
    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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment