Skip to content

Instantly share code, notes, and snippets.

@fulibacsi
Last active January 25, 2017 17:55
Show Gist options
  • Save fulibacsi/6cfb2d668bd38850e29b5865370c2f05 to your computer and use it in GitHub Desktop.
Save fulibacsi/6cfb2d668bd38850e29b5865370c2f05 to your computer and use it in GitHub Desktop.
Dynamic plotting class with multiline support
%matplotlib notebook
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import colors as mcolors
class DynamicPlot():
colors = list(mcolors.cnames.values())
def __init__(self, data):
self.figure, self.ax = plt.subplots(figsize=(12, 10))
self.lines = {}
for i, key in enumerate(data):
self.lines[key], = self.ax.plot([],[], '-', color=self.colors[i], label=key)
legend = self.ax.legend(loc='upper left', shadow=True)
self.ax.set_autoscale_on(True)
def preprocess_data(self, data):
x = [ts for reqnum, reqtime, ts in data if reqnum != 0]
y = [reqtime / reqnum for reqnum, reqtime, ts in data if reqnum != 0]
return x, y
def update(self, data):
for key, values in data.items():
x, y = self.preprocess_data(values)
self.lines[key].set_xdata(x)
self.lines[key].set_ydata(y)
self.ax.relim()
self.ax.autoscale_view()
self.figure.canvas.draw()
init_data = {'a': [], 'b': []}
D = [{'a': [(1, 2, 1), (3, 4, 2), (4, 5, 3)]
'b': [(5, 6, 1), (7, 8, 2), (9, 10, 3)]},
{'a': [(3, 4, 2), (4, 5, 3), (5, 6, 4)]
'b': [(7, 8, 2), (9, 10, 3), (10, 11, 4)]},]
plot = DynamicPlot(init_data)
for d in D:
plot.update(d)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment