Hello,

I am trying to make a GUI interface for a plotting routine written with matplotlib. The GUI is in PyGtk. One of the things I would like to have is a cross hair through the mouse cursor (a vertical and horizontal line intersecting at the mouse) which moves along with the cursor (as long as it's on the plot) I have used matplotlib.backend_gtkagg to embed the figure into a gtk widget:

#!/usr/bin/python
#Filename: foo.py


import pygtk
pygtk.require('2.0')
import gtk
import matplotlib
matplotlib.use('GtkAgg')
from matplotlib.backends.backend_gtkagg import FigureCanvasGTKAgg as Canvas
import pylab


class PyApp(gtk.Window):
   def __init__(self):
      super(PyApp, self).__init__()

      self.fig = matplotlib.pyplot.figure()
      self.ax = self.fig.add_subplot(1,1,1)     
      self.canvas = Canvas(self.fig)
      self.canvas.set_size_request(800,600)
      self.canvas.mpl_connect('motion_notify_event', self.on_drawarea)
      
      self.pltbox = gtk.VBox(False, 0)
      self.pltbox.pack_start(self.canvas)
      self.add(self.pltbox)
      self.connect("destroy",gtk.main_quit)

      self.show_all()      

   def on_drawarea(self,event):
      if event.xdata != None:
         print event.xdata,event.ydata

PyApp()
gtk.main()

Does anybody have any ideas on how to help me? Thank you!

Hello,

I ended up figuring it out, I found out that matplotlib's FigureCanvasGTKAgg class is treated just like a gtk.DrawingArea. So I was able to get what I needed using Cairo:

#!/usr/bin/python
#Filename: foobar.py


import pygtk
pygtk.require('2.0')
import gtk
import matplotlib
matplotlib.use('GtkAgg')
from matplotlib.backends.backend_gtkagg import FigureCanvasGTKAgg as Canvas
import pylab
import cairo


class PyApp(gtk.Window):
   def __init__(self):
      super(PyApp, self).__init__()

      self.set_position(gtk.WIN_POS_CENTER)

      self.fig = matplotlib.pyplot.figure()
      self.ax = self.fig.add_subplot(1,1,1)     
      self.canvas = Canvas(self.fig)
      self.ax.plot([0,1,2,3,4],[0,1,4,9,16])
      
 
 
      
      self.canvas.set_size_request(800,600)
      
      self.canvas.mpl_connect('motion_notify_event', self.on_drawarea)
      self.canvas.connect("expose-event",self.expose)

      
      self.pltbox = gtk.VBox(False, 0)
      self.pltbox.pack_start(self.canvas)
      self.add(self.pltbox)
      self.connect("destroy",gtk.main_quit)
      self.flag = False
      self.show_all()      

   def on_drawarea(self,event):
      if event.xdata != None:         
         self.x = event.x
         self.y = event.y
         self.flag = True
         self.canvas.queue_draw()
         
      else:
         self.flag = False
         self.canvas.queue_draw()
         
         

   def expose(self, widget, event):
      cr = widget.window.cairo_create()
      cr.set_line_width(1)
      cr.set_source_rgb(1.0,0.0,0.0)
      if self.flag:
         cr.move_to(float(self.x),0.0)
         cr.line_to(float(self.x),600.0)
         cr.move_to(0.0,600-float(self.y))
         cr.line_to(800.0,600-float(self.y))
         cr.stroke_preserve()
         self.show_all()
 
              
PyApp()
gtk.main()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.