Skip to content
Snippets Groups Projects
Commit 1ed3b28b authored by Julian Lenz's avatar Julian Lenz
Browse files

Added the Player class to plotting.py

parent 14607d8b
No related branches found
No related tags found
No related merge requests found
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Mar 20 11:01:38 2020
@author: lenz
This is a simple showcase of the Player class in pyVis.plotting.
Full credit goes to ImportanceOfBeingErnest from stackoverflow as this is
copied from his answer here: https://stackoverflow.com/a/46327978.
"""
import numpy as np
import matplotlib.pyplot as plt
import pyVis
pyVis.styling.defaultStyling()
fig, ax = plt.subplots()
x = np.linspace(0,6*np.pi, num=100)
y = np.sin(x)
ax.plot(x,y)
point, = ax.plot([],[], marker="o", color="crimson", ms=15)
def update(i):
point.set_data(x[i],y[i])
ani = pyVis.plotting.Player(fig, update, maxi=len(y)-1)
plt.show()
\ No newline at end of file
......@@ -11,6 +11,9 @@ import matplotlib.cm as cm
from matplotlib.pyplot import figure
from matplotlib import rcParams
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import mpl_toolkits.axes_grid1
import matplotlib.widgets
from .styling import Fig,tight,newFig,ax,colors,names
from .aux import iterTest,gaussian
......@@ -879,4 +882,97 @@ def alignTicks(a1,a2,whichTicks='major',whichAxis='y',ranges='flex'):
else:
raise Exception('This is really weird.')
return None
\ No newline at end of file
return None
class Player(FuncAnimation):
'''
This class sets up a simple animation with forward/backwards playing, stop
and stepwise increment buttons as well as a timeline.
Full credit goes to ImportanceOfBeingErnest from stackoverflow as this is
copied from his answer here: https://stackoverflow.com/a/46327978.
'''
def __init__(self, fig, func, frames=None, init_func=None, fargs=None,
save_count=None, mini=0, maxi=100, pos=(0.125, 0.92), **kwargs):
self.i = 0
self.min=mini
self.max=maxi
self.runs = True
self.forwards = True
self.fig = fig
self.func = func
self.setup(pos)
FuncAnimation.__init__(self,self.fig, self.update, frames=self.play(),
init_func=init_func, fargs=fargs,
save_count=save_count, **kwargs )
def play(self):
while self.runs:
self.i = self.i+self.forwards-(not self.forwards)
if self.i > self.min and self.i < self.max:
yield self.i
else:
self.stop()
yield self.i
def start(self):
self.runs=True
self.event_source.start()
def stop(self, event=None):
self.runs = False
self.event_source.stop()
def forward(self, event=None):
self.forwards = True
self.start()
def backward(self, event=None):
self.forwards = False
self.start()
def oneforward(self, event=None):
self.forwards = True
self.onestep()
def onebackward(self, event=None):
self.forwards = False
self.onestep()
def onestep(self):
if self.i > self.min and self.i < self.max:
self.i = self.i+self.forwards-(not self.forwards)
elif self.i == self.min and self.forwards:
self.i+=1
elif self.i == self.max and not self.forwards:
self.i-=1
self.func(self.i)
self.slider.set_val(self.i)
self.fig.canvas.draw_idle()
def setup(self, pos):
playerax = self.fig.add_axes([pos[0],pos[1], 0.64, 0.04])
divider = mpl_toolkits.axes_grid1.make_axes_locatable(playerax)
bax = divider.append_axes("right", size="80%", pad=0.05)
sax = divider.append_axes("right", size="80%", pad=0.05)
fax = divider.append_axes("right", size="80%", pad=0.05)
ofax = divider.append_axes("right", size="100%", pad=0.05)
sliderax = divider.append_axes("right", size="500%", pad=0.07)
self.button_oneback = matplotlib.widgets.Button(playerax, label=u'$\u29CF$')
self.button_back = matplotlib.widgets.Button(bax, label=u'$\u25C0$')
self.button_stop = matplotlib.widgets.Button(sax, label=u'$\u25A0$')
self.button_forward = matplotlib.widgets.Button(fax, label=u'$\u25B6$')
self.button_oneforward = matplotlib.widgets.Button(ofax, label=u'$\u29D0$')
self.button_oneback.on_clicked(self.onebackward)
self.button_back.on_clicked(self.backward)
self.button_stop.on_clicked(self.stop)
self.button_forward.on_clicked(self.forward)
self.button_oneforward.on_clicked(self.oneforward)
self.slider = matplotlib.widgets.Slider(sliderax, '',
self.min, self.max, valinit=self.i)
self.slider.on_changed(self.set_pos)
def set_pos(self,i):
self.i = int(self.slider.val)
self.func(self.i)
def update(self,i):
self.slider.set_val(i)
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment