Wednesday, 2 October 2013

Using matplotlib picker to change axis limits

Using matplotlib picker to change axis limits

I am working on a project where I create a plot that is embedded in a GTK
container and I need to be able to click the limits of the x and y axis to
change their bounds.
This is what I have to create the picking event so far
def makepickers(self):
xaxis = self.ax.get_xticklabels()
yaxis = self.ax.get_yticklabels()
self.axlbls = (xaxis[0], xaxis[-1], yaxis[0], yaxis[-1])
for entry in self.axlbls:
entry.set_picker(True)
Gets the tick label and sets the picker property to true for the upper and
lower limits of each axis.
On a double click for the limit I create a gtk dialog that pops up and
asks for an input on the limit which then gets passed to the axes and
resizes the graph and remakes the pickers
def onpick(self, event):
pickedtick = event.artist
if event.mouseevent.dblclick:
dialog = gtk.Dialog(title='Set Limits',
parent=None,
flags=0,
buttons=(gtk.STOCK_OK, gtk.RESPONSE_OK))
lbl = gtk.Label("Set Axis Limit")
entry = gtk.Entry()
dialog.vbox.pack_start(lbl, True, True, 0)
dialog.vbox.pack_start(entry, True, True, 0)
entry.show()
lbl.show()
response = dialog.run()
if response == gtk.RESPONSE_OK:
newlim = float(entry.get_text())
dialog.destroy()
idx = self.axlbls.index(pickedtick)
if self.lim_option_x == 'Manual':
if idx == 0: # x lower limit
self.ax.set_xlim(left=newlim)
if idx == 1: # x upper limit
self.ax.set_xlim(right=newlim)
if self.lim_option_y == 'Manual':
if idx == 3: # y upper limit
self.ax.set_ylim(top=newlim)
if idx == 2: # y lower limit
self.ax.set_ylim(bottom=newlim)
del self.axlbls
self.ax.relim()
self.ax.autoscale_view()
self.canvas.draw()
self.makepickers()
The 'Manual' option is determined by a checkbox (on for each axis) which
lies within the class.
My problem is it works for the first few times when resizing the limits,
but every so often an error appears where it states that,
idx = self.axlbls.index(pickedtick)
ValueError: <matplotlib.text.Text object at 0x02D00870> is not in list
This is also not a consistent error. Every time the canvas is redrawn or a
new point is added to the graph which might redraw the axis I remake the
pickers. Not sure if anyone has an ideas on why this is coming up. Help
would be much appreciated.

No comments:

Post a Comment