Skip to content

Instantly share code, notes, and snippets.

@greydanus
Created October 14, 2016 18:20
Show Gist options
  • Select an option

  • Save greydanus/f6eee59eaf1d90fcb3b534a25362cea4 to your computer and use it in GitHub Desktop.

Select an option

Save greydanus/f6eee59eaf1d90fcb3b534a25362cea4 to your computer and use it in GitHub Desktop.

Revisions

  1. greydanus revised this gist Oct 14, 2016. No changes.
  2. greydanus created this gist Oct 14, 2016.
    27 changes: 27 additions & 0 deletions dynamic_plotting.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    "Dynamic plotting in matplotlib. Copy and paste into a Jupyter notebook."
    # written October 2016 by Sam Greydanus
    %matplotlib notebook
    import matplotlib.pyplot as plt
    import numpy as np
    import time

    def plt_dynamic(x, y, ax, colors=['b']):
    for color in colors:
    ax.plot(x, y, color)
    fig.canvas.draw()

    fig,ax = plt.subplots(1,1)
    ax.set_xlabel('X') ; ax.set_ylabel('Y')
    ax.set_xlim(0,360) ; ax.set_ylim(-1,1)
    xs, ys = [], []

    # this is any loop for which you want to plot dynamic updates.
    # in my case, I'm plotting loss functions for neural nets
    for x in range(360):
    y = np.sin(x*np.pi/180)
    xs.append(x)
    ys.append(y)
    if x % 30 == 0:
    plt_dynamic(xs, ys, ax)
    time.sleep(.2)
    plt_dynamic(xs, ys, ax)