Dynamically adding a vertical line to matplotlib plot - python

I'm trying to add vertical lines to a matplotlib plot dynmically when a user clicks on a particular point.
import matplotlib.pyplot as plt
import matplotlib.dates as mdate
class PointPicker(object):
def __init__(self,dates,values):
self.fig = plt.figure()
self.ax = self.fig.add_subplot(111)
self.lines2d, = self.ax.plot_date(dates, values, linestyle='-',picker=5)
self.fig.canvas.mpl_connect('pick_event', self.onpick)
self.fig.canvas.mpl_connect('key_press_event', self.onpress)
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
print self.ax.axvline(x=x, visible=True)
x = mdate.num2date(x)
print x,y,type(x)
if __name__ == '__main__':
import numpy as np
import datetime
dates=[datetime.datetime.now()+i*datetime.timedelta(days=1) for i in range(100)]
values = np.random.random(100)
plt.ion()
p = PointPicker(dates,values)
plt.show()
Here's an (almost) working example. When I click a point, the onpick method is indeed called and the data seems to be correct, but no vertical line shows up. What do I need to do to get the vertical line to show up?
Thanks

You need to update the canvas drawing (self.fig.canvas.draw()):
def onpick(self,event):
x = event.mouseevent.xdata
y = event.mouseevent.ydata
L = self.ax.axvline(x=x)
self.fig.canvas.draw()

Related

Getting coordinates of the closest data point on matplotlib plot

I am using matplotlib with NavigationToolbar2QT. The toolbar is showing the position of the cursor. But I would like that the cursor snaps to the nearest data point (when close enough) or simply show the coordinate of nearest data point. Can that be somehow arranged?
If you are working with large sets of points, I advice you to use CKDtrees:
import matplotlib.pyplot as plt
import numpy as np
import scipy.spatial
points = np.column_stack([np.random.rand(50), np.random.rand(50)])
fig, ax = plt.subplots()
coll = ax.scatter(points[:,0], points[:,1])
ckdtree = scipy.spatial.cKDTree(points)
I refactored kpie's answer here little bit. Once ckdtree is created, you can identify closest points instantly and various kind of information about them with a little effort:
def closest_point_distance(ckdtree, x, y):
#returns distance to closest point
return ckdtree.query([x, y])[0]
def closest_point_id(ckdtree, x, y):
#returns index of closest point
return ckdtree.query([x, y])[1]
def closest_point_coords(ckdtree, x, y):
# returns coordinates of closest point
return ckdtree.data[closest_point_id(ckdtree, x, y)]
# ckdtree.data is the same as points
Interactive display of cursor position.
If you want coordinates of the closest point to be displayed on Navigation Toolbar:
def val_shower(ckdtree):
#formatter of coordinates displayed on Navigation Bar
return lambda x, y: '[x = {}, y = {}]'.format(*closest_point_coords(ckdtree, x, y))
plt.gca().format_coord = val_shower(ckdtree)
plt.show()
Using events.
If you want another kind of interactivity, you can use events:
def onclick(event):
if event.inaxes is not None:
print(closest_point_coords(ckdtree, event.xdata, event.ydata))
fig.canvas.mpl_connect('motion_notify_event', onclick)
plt.show()
You could subclass NavigationToolbar2QT and override the mouse_move handler. The xdata and ydata attributes contain the current mouse position in plot coordinates. You can snap that to the closest data point before passing the event to the base class mouse_move handler.
Full example, with highlighting of the closest point in the plot as a bonus:
import sys
import numpy as np
from matplotlib.backends.qt_compat import QtWidgets
from matplotlib.backends.backend_qt5agg import FigureCanvas, NavigationToolbar2QT
from matplotlib.figure import Figure
class Snapper:
"""Snaps to data points"""
def __init__(self, data, callback):
self.data = data
self.callback = callback
def snap(self, x, y):
pos = np.array([x, y])
distances = np.linalg.norm(self.data - pos, axis=1)
dataidx = np.argmin(distances)
datapos = self.data[dataidx,:]
self.callback(datapos[0], datapos[1])
return datapos
class SnappingNavigationToolbar(NavigationToolbar2QT):
"""Navigation toolbar with data snapping"""
def __init__(self, canvas, parent, coordinates=True):
super().__init__(canvas, parent, coordinates)
self.snapper = None
def set_snapper(self, snapper):
self.snapper = snapper
def mouse_move(self, event):
if self.snapper and event.xdata and event.ydata:
event.xdata, event.ydata = self.snapper.snap(event.xdata, event.ydata)
super().mouse_move(event)
class Highlighter:
def __init__(self, ax):
self.ax = ax
self.marker = None
self.markerpos = None
def draw(self, x, y):
"""draws a marker at plot position (x,y)"""
if (x, y) != self.markerpos:
if self.marker:
self.marker.remove()
del self.marker
self.marker = self.ax.scatter(x, y, color='yellow')
self.markerpos = (x, y)
self.ax.figure.canvas.draw()
class ApplicationWindow(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self._main = QtWidgets.QWidget()
self.setCentralWidget(self._main)
layout = QtWidgets.QVBoxLayout(self._main)
canvas = FigureCanvas(Figure(figsize=(5,3)))
layout.addWidget(canvas)
toolbar = SnappingNavigationToolbar(canvas, self)
self.addToolBar(toolbar)
data = np.random.randn(100, 2)
ax = canvas.figure.subplots()
ax.scatter(data[:,0], data[:,1])
self.highlighter = Highlighter(ax)
snapper = Snapper(data, self.highlighter.draw)
toolbar.set_snapper(snapper)
if __name__ == "__main__":
qapp = QtWidgets.QApplication(sys.argv)
app = ApplicationWindow()
app.show()
qapp.exec_()
The following code will print the coordinates of the dot closest to the mouse when you click.
import matplotlib.pyplot as plt
import numpy as np
np.random.seed(19680801)
N = 50
x = np.random.rand(N)
y = np.random.rand(N)
fig,ax = plt.subplots()
plt.scatter(x, y)
points = list(zip(x,y))
def distance(a,b):
return(sum([(k[0]-k[1])**2 for k in zip(a,b)])**0.5)
def onclick(event):
dists = [distance([event.xdata, event.ydata],k) for k in points]
print(points[dists.index(min(dists))])
fig.canvas.mpl_connect('button_press_event', onclick)
plt.show()
Another possibility is to use the picking support axes already have. See this section in the event handling docs.
Jim

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()

python - how can I display cursor on all axes vertically but only on horizontally on axes with mouse pointer on top

I would like the cursor to be visible across all axes vertically but only visible horizontally for the axis that the mouse pointer is on.
This is a code exert of what I am using at the moment.
import matplotlib.pyplot as plt
from matplotlib.widgets import MultiCursor
fig = plt.figure(facecolor='#07000d')
ax1 = plt.subplot2grid((2,4), (0,0), rowspan=1,colspan=4, axisbg='#aaaaaa')
ax2 = plt.subplot2grid((2,4), (1,0), rowspan=1,colspan=4, axisbg='#aaaaaa')
multi = MultiCursor(fig.canvas, (ax1, ax2), color='r', lw=.5, horizOn=True, vertOn=True)
plt.show()
This is what I have. What I would like is to have the horizontal cursor to be only visible for the axis that mouse pointer is on but have the vertical visible for both.
So I came up with a solution. I made my own custom cursor using mouse events wrapped in a class. Works great. You can add your axes in an array/list.
import numpy as np
import matplotlib.pyplot as plt
class CustomCursor(object):
def __init__(self, axes, col, xlimits, ylimits, **kwargs):
self.items = np.zeros(shape=(len(axes),3), dtype=np.object)
self.col = col
self.focus = 0
i = 0
for ax in axes:
axis = ax
axis.set_gid(i)
lx = ax.axvline(ymin=ylimits[0],ymax=ylimits[1],color=col)
ly = ax.axhline(xmin=xlimits[0],xmax=xlimits[1],color=col)
item = list([axis,lx,ly])
self.items[i] = item
i += 1
def show_xy(self, event):
if event.inaxes:
self.focus = event.inaxes.get_gid()
for ax in self.items[:,0]:
self.gid = ax.get_gid()
for lx in self.items[:,1]:
lx.set_xdata(event.xdata)
if event.inaxes.get_gid() == self.gid:
self.items[self.gid,2].set_ydata(event.ydata)
self.items[self.gid,2].set_visible(True)
plt.draw()
def hide_y(self, event):
for ax in self.items[:,0]:
if self.focus == ax.get_gid():
self.items[self.focus,2].set_visible(False)
if __name__ == '__main__':
fig = plt.figure(facecolor='#07000d')
ax1 = plt.subplot2grid((2,4), (0,0), rowspan=1,colspan=4, axisbg='#aaaaaa')
ax2 = plt.subplot2grid((2,4), (1,0), rowspan=1,colspan=4, axisbg='#aaaaaa')
cc = CustomCursor([ax1,ax2], col='red', xlimits=[0,100], ylimits=[0,100], markersize=30,)
fig.canvas.mpl_connect('motion_notify_event', cc.show_xy)
fig.canvas.mpl_connect('axes_leave_event', cc.hide_y)
plt.tight_layout()
plt.show()

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