Created
January 16, 2017 00:09
-
-
Save Kruptein/34a2f98a4d8147653f4fc3bbb57c732e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import queue | |
from threading import Thread | |
import logging | |
import numpy as np | |
import time | |
from bokeh.models import Range1d | |
from bokeh.plotting import figure, curdoc | |
from bokeh.models.sources import ColumnDataSource | |
from bokeh.client import push_session | |
from datetime import datetime | |
logger = logging.getLogger('Backend') | |
class Plot(Thread): | |
def __init__(self): | |
super().__init__(daemon=True) | |
self.x = np.zeros(10) | |
self.y1 = np.zeros(10) | |
self.y2 = np.zeros(10) | |
self.last_data = (0, 0) | |
self.figure = figure(plot_width=800, plot_height=400, y_range=Range1d(-0.1, 1.1)) | |
self.figure.xaxis.axis_label = "Time passed (s)" | |
self.figure.yaxis.axis_label = "Normalized value" | |
datacoords = ColumnDataSource(data=dict(x=self.x, y1=self.y1, y2=self.y2)) | |
self.linea = self.figure.line("x", "y1", line_color="red", source=datacoords, legend="Potential") | |
self.lineb = self.figure.line("x", "y2", source=datacoords, legend="Current") | |
self.start_time = datetime.now() | |
def update_data(self, y1, y2): | |
self.last_data = (y1, y2) | |
def run(self): | |
# open a session to keep our local document in sync with server | |
session = push_session(curdoc()) | |
# curdoc().add_periodic_callback(self.update, 1000) # period in ms | |
# session.loop_until_complete() | |
session.show(self.figure) | |
self.update() | |
def update(self): | |
while True: | |
timestamp = datetime.now() | |
new_x = ((timestamp - self.start_time).total_seconds()) | |
self.x = np.roll(self.x, -1) | |
self.y1 = np.roll(self.y1, -1) | |
self.y2 = np.roll(self.y2, -1) | |
self.x[-1] = new_x | |
self.y1[-1] = self.last_data[0] | |
self.y2[-1] = self.last_data[1] | |
self.linea.data_source.data["x"] = self.x | |
self.linea.data_source.data["y1"] = self.y1 | |
self.lineb.data_source.data["y2"] = self.y2 | |
push_session(curdoc()) | |
time.sleep(0.1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment