Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save swyoon/0c0e62d7a705fd92a8a2c3873062ad54 to your computer and use it in GitHub Desktop.
Save swyoon/0c0e62d7a705fd92a8a2c3873062ad54 to your computer and use it in GitHub Desktop.
Dynamically update plots in Jupyter lab
# source: https://stackoverflow.com/a/52672859/5554394
from IPython.display import clear_output
from matplotlib import pyplot as plt
import collections
%matplotlib inline
def live_plot(data_dict, figsize=(7,5), title=''):
clear_output(wait=True)
plt.figure(figsize=figsize)
for label,data in data_dict.items():
plt.plot(data, label=label)
plt.title(title)
plt.grid(True)
plt.xlabel('epoch')
plt.legend(loc='center left') # the plot evolves to the right
plt.show();
# Then in a loop you populate a dictionary and you pass it to live_plot():
data = collections.defaultdict(list)
for i in range(100):
data['foo'].append(np.random.random())
data['bar'].append(np.random.random())
data['baz'].append(np.random.random())
live_plot(data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment