Plot fast changing data using OOP approach - python

I'm trying to plot the results from each iteration of an algorithm (data points are obtained quite fast). I´ve already looked at matplotlib.animation and a bunch of solutions using other packages but none of them uses an OOP approach.
I came up with this code:
import matplotlib.animation as animation
import random
class LivePlot:
def __init__(self, title, x_label, y_label):
self.x = []
self.y = []
self.fig = plt.figure()
self.ax = self.fig.add_subplot(1, 1, 1)
# Create a blank line (will be updated in animate)
self.line, = self.ax.plot([], [])
# Add labels
plt.xlabel(x_label)
plt.ylabel(y_label)
plt.title(title)
self.anim = animation.FuncAnimation(self.fig, self.animate,
init_func=self.__init,
blit=True)
def __init(self):
self.line.set_data([], [])
return self.line,
def animate(self, i):
# Update line with new Y values
self.line.set_data(self.x, self.y)
return self.line,
# Set up plot to call animate() function periodically
def plot(self, xs, ys):
self.x.append(xs)
self.y.append(ys)
plt.show()
if __name__ == '__main__':
pl = LivePlot(title='test', x_label='iter', y_label='score')
for x in range(100):
y = random.randint(1,4)
pl.plot(x, y)
It doesn´t work, the plot window disappear almost immediately without plotting any data.

Related

Is there a way to reset fig.canvas.manager.set_window_title()? (overlapping window titles)

I tried to do a wrap function around the plotting class, however when I save my plots first function looks good, however in second fig_name overlaps on each other. I am guessing the problems is fig.canvas.manager.set_window_title(fig_name)(I use this name function because I might use more than one axis plot in one figure). Here is my image that I would like to fix:
My example code:
import numpy as np
import matplotlib.pyplot as plt
# figure creating and saving wrapper
def figure_wrapper(func):
def wrapper(*args,**kwargs):
fig, ax, x_label, y_label, axs_name, fig_name = func(*args,**kwargs)
for ax_name in axs_name:
ax.set_title(ax_name, fontweight='bold', size='22')
ax.legend()
ax.set_xlabel(x_label, fontweight='bold')
ax.set_ylabel(y_label, fontweight='bold')
fig.canvas.manager.set_window_title(fig_name)
fig.savefig(fig_name, bbox_inches='tight', dpi=1200)
plt.close(fig)
return wrapper
# plotting class
class plotting():
def __init__(self):
self.fig=plt.figure()
#figure_wrapper
def plot(self,x,y):
ax_name = 'plot1'
axs_name = [ax_name]
fig_name = "Some nice name for plot figure 1"
ax = self.fig.add_subplot(label=ax_name)
x_label = "R \u03A9"
y_label = "L \u03BCm"
ax.plot(x,y,label= '1')
ax.set_title(ax_name, fontweight='bold', size='22')
return self.fig, ax, x_label, y_label, axs_name, fig_name
#figure_wrapper
def scatter(self,x,y,):
ax_name = 'scatter1'
axs_name = [ax_name]
fig_name = "Even better name for plot figure 2"
ax = self.fig.add_subplot(label=ax_name)
x_label = "R \u03A9"
y_label = "L \u03BCm"
ax.scatter(x, y,label= '2')
return self.fig, ax, x_label, y_label, axs_name, fig_name
# data for plotting
x = np.linspace(0, 10, 10)
y = np.linspace(20, 30, 10)
x1 = np.linspace(20, 10, 10)
y1 = np.linspace(60, 30, 10)
# execution of class
plotting = plotting()
plotting.plot(x,y)
plotting.scatter(x,y)
You have to create new figure for every plot
#figure_wrapper
def plot(self, x, y):
self.fig = plt.figure()
# ... code ...
#figure_wrapper
def scatter(self, x, y):
self.fig = plt.figure()
# ... code ...
Or you have to clear() existing figure.
#figure_wrapper
def plot(self, x, y):
self.fig.clear()
# ... code ...
#figure_wrapper
def scatter(self, x, y):
self.fig.clear()
# ... code ...
Problem can be because every plot uses self.fig.add_subplot(label=ax_name) so it adds new plot to existing plot. And it has nothing to do with set_title()

Matplotlib Animation does not update within class

I'm writing a simple class to plot a sensor value in real-time; however, the animation does not run within the class.
I've tried to return the animation object to have an instance outside of the class but this does not work.
To my understanding, this is the same issue as raised in GitHub #1656
import datetime as dt
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from random import random
class Animate:
def __init__(self, sensor):
# Create figure for plotting
self.fig = plt.figure()
self.ax = self.fig.add_subplot(1, 1, 1)
self.xs = []
self.ys = []
self.ylabel = sensor
self.readings = 20
# This function is called periodically from FuncAnimation
def _update(self, i, xs, ys):
# Get sensor value
value = random()
# Add x and y to lists
self.xs.append(dt.datetime.now().strftime('%H:%M:%S.%f'))
self.ys.append(value)
# Limit x and y lists to 20 items
self.xs = self.xs[-self.readings:]
self.ys = self.ys[-self.readings:]
# Draw x and y lists
self.ax.clear()
self.ax.plot(xs, ys)
# Format plot
plt.xticks(rotation=45, ha='right')
plt.subplots_adjust(bottom=0.30)
plt.title(self.ylabel + ' over Time')
plt.ylabel(self.ylabel)
def start(self):
print('Starting')
# Set up plot to call animate() function periodically
self.anim = animation.FuncAnimation(self.fig, self._update, fargs=(self.xs, self.ys), interval=200)
plt.show();
rand = Animate('Torque')
rand.start();
your variables xs and ys are already named self.xs and self.ys, which are accessible in the class namespace; you do not need to pass them to self.update
import datetime as dt
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from random import random
class Animate:
def __init__(self, sensor):
# Create figure for plotting
self.fig = plt.figure()
self.ax = self.fig.add_subplot(1, 1, 1)
self.xs = []
self.ys = []
self.ylabel = sensor
self.readings = 20
# This function is called periodically from FuncAnimation
def _update(self, i):
# Read temperature (Celsius) from TMP102
temp_c = random()
# Add x and y to lists
self.xs.append(dt.datetime.now().strftime('%H:%M:%S.%f'))
self.ys.append(temp_c)
# Limit x and y lists to 20 items
self.xs = self.xs[-self.readings:]
self.ys = self.ys[-self.readings:]
# Draw x and y lists
self.ax.clear()
self.ax.plot(self.xs, self.ys)
# Format plot
plt.xticks(rotation=45, ha='right')
plt.subplots_adjust(bottom=0.30)
plt.title(self.ylabel + ' over Time')
plt.ylabel(self.ylabel)
def start(self):
print('Starting')
# Set up plot to call animate() function periodically
self.anim = animation.FuncAnimation(self.fig, self._update, interval=200)
plt.show()
rand = Animate('Torque')
rand.start()

Point picker event_handler drawing line and displaying coordinates in matplotlib

I have the following class that draws a vertical line through the y-axis, so that when I click on it a horizontal line gets drawn at the location.
My goal is to get the y-coordinate to actually print right at the y-axis where the horizontal line gets drawn. For testing, I am trying to print a title with the y-ccordinate, but it is not working as expected.
What I am trying to really accomplish is to make the y-axis on a bar plot clickable so that the user can select a point on the y-axis, and then a bunch of "stuff" happens (including the drawing of a horizontal line). I really see no other way to make this happen, other than drawing a plottable vertical line through the y-axis to make it pickable. This seems rather kludgey, but I have not had any success with any other methods.
import matplotlib.pyplot as plt
class PointPicker(object):
def __init__(self):
self.fig = plt.figure()
self.ax = self.fig.add_subplot(111)
self.lines2d, = self.ax.plot((0, 0), (-6000, 10000), 'k-', linestyle='-',picker=5)
self.fig.canvas.mpl_connect('pick_event', self.onpick)
self.fig.canvas.mpl_connect('key_press_event', self.onpress)
fig.canvas.mpl_connect('button_press_event', self.onclick)
def onpress(self, event):
"""define some key press events"""
if event.key.lower() == 'q':
sys.exit()
def onpick(self,event):
x = event.mouseevent.xdata
y = event.mouseevent.ydata
L = self.ax.axhline(y=y)
print(y)
ax.axvspan(0, 0, facecolor='y', alpha=0.5, picker=10)
self.fig.canvas.draw()
def onclick(event):
self.fig.canvas.set_title('Selected item came from {}'.format(event.ydata))
print(event.xdata, event.ydata)
if __name__ == '__main__':
plt.ion()
p = PointPicker()
plt.show()
Assuming there is no ther way to achieve my end result, all is well with this method, except I cannot for the life of me get the title to print (using the self.fig.canvas.set_title('Selected item came from {}'.format(event.ydata)).
You can use the 'button_press_event' to connect to a method, which verifies that the click has happened close enough to the yaxis spine and then draws a horizontal line using the clicked coordinate.
import matplotlib.pyplot as plt
class PointPicker(object):
def __init__(self, ax, clicklim=0.05):
self.fig=ax.figure
self.ax = ax
self.clicklim = clicklim
self.horizontal_line = ax.axhline(y=.5, color='y', alpha=0.5)
self.text = ax.text(0,0.5, "")
print self.horizontal_line
self.fig.canvas.mpl_connect('button_press_event', self.onclick)
def onclick(self, event):
if event.inaxes == self.ax:
x = event.xdata
y = event.ydata
xlim0, xlim1 = ax.get_xlim()
if x <= xlim0+(xlim1-xlim0)*self.clicklim:
self.horizontal_line.set_ydata(y)
self.text.set_text(str(y))
self.text.set_position((xlim0, y))
self.fig.canvas.draw()
if __name__ == '__main__':
fig = plt.figure()
ax = fig.add_subplot(111)
ax.bar([0,2,3,5],[4,5,1,3], color="#dddddd")
p = PointPicker(ax)
plt.show()

How can I create plots before creating animation?

I would like to plot data using multiprocessing and then creating animation with the plotted data. I mean something like this:
frames = []
def get_frames()
...
[index, frame] = mp_queue.get()
frames[index]=frame
def get_frames_process(queue, index, x_vals, y_vals):
frame = plt.scatter(x_vals[index], y_vals[index])
queue.put([index, frame])
def animate(frame):
frames.pop(0)
plt.plot(frame)
anim = animation.FuncAnimation(fig, animate, frames=frames)
Or also, there is a way of use FuncAnimation with multiprocessing?
From the FuncAnimation documentation:
frames can be a generator, an iterable, or a number of frames.
I suggest you write a generator function that uses multiprocessing to iterate through your frame calculations. Here's an example:
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from multiprocessing import Pool
def calc_fib(n):
if n in (0, 1):
return 1
return calc_fib(n-1) + calc_fib(n-2)
class FibonacciAnimation(object):
def __init__(self, count):
self.count = count
self.line, = plt.plot([], [], 'r-')
self.pool = Pool(8)
def update(self, n):
self.line.set_data(self.xs, self.ys)
return self.line,
def frames(self):
for n in range(self.count):
self.xs = range(n)
self.ys = self.pool.map(calc_fib, self.xs)
yield
fig = plt.figure()
fib = FibonacciAnimation(30)
plt.xlim(0, fib.count)
plt.ylim(0, 1000000)
plt.title('Fibonacci Animation')
fib_ani = animation.FuncAnimation(fig, fib.update, fib.frames,
interval=50, blit=True)
plt.show()
I made the Fibonacci calculation deliberately inefficient so you can compare map to self.pool.map and see the effect of multiprocessing.

Saving scatterplot animations with matplotlib

I've been trying to save an animated scatterplot with matplotlib, and I would prefer that it didn't require totally different code for viewing as an animated figure and for saving a copy. The figure shows all the datapoints perfectly after the save completes.
This code is a modified version of Giggi's on Animating 3d scatterplot in matplotlib, with a fix for the colors from Yann's answer on Matplotlib 3D scatter color lost after redraw (because colors will be important on my video, so I want to make sure they work).
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
FLOOR = -10
CEILING = 10
class AnimatedScatter(object):
def __init__(self, numpoints=5):
self.numpoints = numpoints
self.stream = self.data_stream()
self.angle = 0
self.fig = plt.figure()
self.fig.canvas.mpl_connect('draw_event',self.forceUpdate)
self.ax = self.fig.add_subplot(111,projection = '3d')
self.ani = animation.FuncAnimation(self.fig, self.update, interval=100,
init_func=self.setup_plot, blit=True,frames=20)
def change_angle(self):
self.angle = (self.angle + 1)%360
def forceUpdate(self, event):
self.scat.changed()
def setup_plot(self):
X = next(self.stream)
c = ['b', 'r', 'g', 'y', 'm']
self.scat = self.ax.scatter(X[:,0], X[:,1], X[:,2] , c=c, s=200, animated=True)
self.ax.set_xlim3d(FLOOR, CEILING)
self.ax.set_ylim3d(FLOOR, CEILING)
self.ax.set_zlim3d(FLOOR, CEILING)
return self.scat,
def data_stream(self):
data = np.zeros(( self.numpoints , 3 ))
xyz = data[:,:3]
while True:
xyz += 2 * (np.random.random(( self.numpoints,3)) - 0.5)
yield data
def update(self, i):
data = next(self.stream)
#data = np.transpose(data)
self.scat._offsets3d = ( np.ma.ravel(data[:,0]) , np.ma.ravel(data[:,1]) , np.ma.ravel(data[:,2]) )
plt.draw()
return self.scat,
def show(self):
plt.show()
if __name__ == '__main__':
a = AnimatedScatter()
a.ani.save("movie.avi", codec='avi')
a.show()
A perfectly valid .avi is generated by this, but it's blank for all of the four seconds except for the axes. The actual figure always shows exactly what I want to see. How can I populate the save function's plots the same way I populate a normally running animation, or is it possible in matplotlib?
EDIT: Using a scatter call in the update (without setting the bounds as in the initializer) causes the .avi to show the axes growing, showing that the data is being run each time, it's just not showing on the video itself.
I am using matplotlib 1.1.1rc with Python 2.7.3.
Remove blit=True from FuncAnimation and animated=True from scatter and it works. I suspect that there is something going wrong with the logic that makes sure only the artists that need to be updated are updated/redrawn between frames (rather than just re-drawing everything).
Below is exactly what I ran and I got the expected output movie:
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
FLOOR = -10
CEILING = 10
class AnimatedScatter(object):
def __init__(self, numpoints=5):
self.numpoints = numpoints
self.stream = self.data_stream()
self.angle = 0
self.fig = plt.figure()
self.fig.canvas.mpl_connect('draw_event',self.forceUpdate)
self.ax = self.fig.add_subplot(111,projection = '3d')
self.ani = animation.FuncAnimation(self.fig, self.update, interval=100,
init_func=self.setup_plot, frames=20)
def change_angle(self):
self.angle = (self.angle + 1)%360
def forceUpdate(self, event):
self.scat.changed()
def setup_plot(self):
X = next(self.stream)
c = ['b', 'r', 'g', 'y', 'm']
self.scat = self.ax.scatter(X[:,0], X[:,1], X[:,2] , c=c, s=200)
self.ax.set_xlim3d(FLOOR, CEILING)
self.ax.set_ylim3d(FLOOR, CEILING)
self.ax.set_zlim3d(FLOOR, CEILING)
return self.scat,
def data_stream(self):
data = np.zeros(( self.numpoints , 3 ))
xyz = data[:,:3]
while True:
xyz += 2 * (np.random.random(( self.numpoints,3)) - 0.5)
yield data
def update(self, i):
data = next(self.stream)
self.scat._offsets3d = ( np.ma.ravel(data[:,0]) , np.ma.ravel(data[:,1]) , np.ma.ravel(data[:,2]) )
return self.scat,
def show(self):
plt.show()
if __name__ == '__main__':
a = AnimatedScatter()
a.ani.save("movie.avi", codec='avi')
a.show()

Categories