Skip to content

Instantly share code, notes, and snippets.

@wassname
Created September 27, 2017 06:14
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save wassname/04e77eb821447705b399e8e7a6d082ce to your computer and use it in GitHub Desktop.
Save wassname/04e77eb821447705b399e8e7a6d082ce to your computer and use it in GitHub Desktop.
Live plot using %matplotlib notebook in jupyter notebook
import numpy as np
from matplotlib import pyplot as plt
class LivePlotNotebook(object):
"""
Live plot using %matplotlib notebook in jupyter notebook
Usage:
```
import time
liveplot = LivePlotNotebook()
x=np.random.random((10,))
for i in range(10):
time.sleep(1)
liveplot.update(
x=x+np.random.random(x.shape)/10,
actions=np.random.randint(0, 3, size=(10,))
)
```
url:
"""
def __init__(self):
%matplotlib notebook
fig,ax = plt.subplots(1,1)
ax.plot([0]*20, label='price')
ax.plot([1]*20, [1]*20, 'o', ms=12,c='gray', label='hold')
ax.plot([0]*20, [0]*20, '^', ms=12,c='blue', label='buy' )
ax.plot([0]*20, [0]*20, 'v', ms=12,c='red', label='sell')
ax.set_xlim(0,1)
ax.set_ylim(0,1)
ax.legend()
ax.set_xlabel('timesteps')
ax.grid()
ax.set_title('actions')
self.ax = ax
self.fig = fig
def update(self, x, actions):
# update price
line = self.ax.lines[0]
line.set_xdata(range(len(x)))
line.set_ydata(x)
# update action plots
for i, line in enumerate(self.ax.lines[1:]):
line.set_xdata(np.argwhere(actions==i).T)
line.set_ydata(x[actions==i])
line.set_marker(['o','^','v'][i])
# update limits
self.ax.set_xlim(0, len(actions))
self.ax.set_ylim(x.min(), x.max())
self.fig.canvas.draw()
# Test
import time
liveplot = LivePlotNotebook()
x=np.random.random((10,))
for i in range(10):
time.sleep(1)
liveplot.update(
x=x+np.random.random(x.shape)/10,
actions=np.random.randint(0, 3, size=(10,))
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment