Skip to content

Instantly share code, notes, and snippets.

@Tillsten
Created June 20, 2018 18:35
Show Gist options
  • Save Tillsten/cf623cee8976070c535710d294f0a54d to your computer and use it in GitHub Desktop.
Save Tillsten/cf623cee8976070c535710d294f0a54d to your computer and use it in GitHub Desktop.
import vispy.scene as scene
import vispy.plot as vp
from vispy.color import Color
import vispy
import vispy.visuals as vv
from qtpy.QtCore import Signal, QTimer
from qtpy.QtWidgets import QWidget, QApplication, QMainWindow
import attr
vispy.use('PyQt5')
@attr.s(auto_attribs=True)
class VpPlotManager:
plot_items : list = []
bg_color : Color = Color('w')
font_size : float = 15
scene_canvas : scene.SceneCanvas = attr.ib()
plot_widget : vp.PlotWidget = attr.ib()
_style_is_set : bool = False
@scene_canvas.default
def sc_default(self):
return scene.SceneCanvas(fullscreen=False)
@plot_widget.default
def pw_default(self):
pw = vp.PlotWidget(bgcolor='k', fg_color='lightgrey')
self.scene_canvas.central_widget.add_widget(pw)
return pw
def addPlot(self, data, name=None, **kwargs):
pw = self.plot_widget
if name is None:
name = f'LinePlot{len(self.plot_items)}'
names = [i.name for i in self.plot_items]
if name in names:
raise
lp = pw.plot(data=data)
if not self._style_is_set:
pw.camera.viewbox.bgcolor = 'k'
pw.xlabel.text = 'Wavelength'
pw.xaxis.axis._text.font_size = self.font_size
pw.yaxis.axis._text.font_size = self.font_size
pw.yaxis.axis._line.set_data(width=2)
pw.xaxis.axis._line.set_data(width=2)
#mv = scene.visuals.GridLines(parent=self.plot_widget.camera)
print(pw.xaxis.axis)
self._style_is_set = True
lp._line.antialias = False
vlp = LinePlot(vp_line_plot=lp, data=data, name=name, **kwargs)
vlp.set_data(data)
self.plot_items.append(vlp)
return vlp
@attr.s(auto_attribs=True)
class LinePlot:
vp_line_plot: vp.LinePlot = attr.ib()
line_width : float = 1.
line_color : Color = attr.Factory(Color)
marker_size : float = 0.
marker_edgewidth = 1
marker_facecolor : Color = Color('w')
marker_edgecolor : Color = Color('grey')
data : object = []
name : str = ''
def __attrs_post_init__(self):
self.set_data(self.data)
#
def set_data(self, data=()):
self.vp_line_plot.set_data(data, width=self.line_width, color=self.line_color,
marker_size=self.marker_size, face_color=self.marker_facecolor,
edge_width=self.marker_edgewidth, edge_color=self.marker_edgecolor,
)
if __name__ == '__main__':
import numpy as np
app = QApplication([])
win = QMainWindow()
vm = VpPlotManager()
vm.scene_canvas.measure_fps()
vlp = vm.addPlot(np.random.randn(2000), line_width=1, line_color='r')
vlp2 = vm.addPlot(np.random.randn(2000)+5, line_color='y')
vlp3 = vm.addPlot(np.random.randn(2000), line_width=1, line_color='lightblue')
vlp4 = vm.addPlot(np.random.randn(2000)+5, line_color='cyan')
#vlp2.line_color = 'r'
vlp.line_color = 'g'
import time
t0 = time.time()
x = np.linspace(0, 2*np.pi, 2000)
def update():
t = 3*(time.time() - t0)
vlp.set_data(np.random.randn(2000)*0.3+np.sin(x*2+4*t))
vlp2.set_data(np.random.randn(2000)*0.3+np.cos(x*4+3*t)+3)
vlp3.set_data(np.random.randn(2000)*0.3+np.sin(x+3.5*t)+5)
vlp4.set_data(np.random.randn(2000)*0.3+np.cos(x*0.5+2*t)+8)
#print(vm.plot_widget.camera.viewbox.rect)
timer = QTimer()
timer.timeout.connect(update)
timer.start(1)
win.setCentralWidget(vm.scene_canvas.native)
print(vm.plot_items[0])
win.show()
app.exec_()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment