This is an archival dump of old wiki content --- see scipy.org for current material.
Please see http://scipy-cookbook.readthedocs.org/

Attachment 'mplwidget.py'

Download

   1 import sys, os, random
   2 from qt import *
   3 
   4 import matplotlib
   5 matplotlib.use('Agg')
   6 import pylab
   7 
   8 from matplotlib.numerix import arange, sin, pi
   9 
  10 from matplotlib.backends.backend_qtagg import FigureCanvasQTAgg as FigureCanvas
  11 from matplotlib.figure import Figure
  12 
  13 
  14 class MatplotlibWidget(FigureCanvas):
  15     """Ultimately, this is a QWidget (as well as a FigureCanvasAgg, etc.)."""
  16     def __init__(self, parent=None, name=None, width=5, height=4, dpi=100, bgcolor=None):
  17 	self.parent = parent
  18 	if self.parent:
  19 		bgc = parent.backgroundBrush().color()
  20 		bgcolor = float(bgc.red())/255.0, float(bgc.green())/255.0, float(bgc.blue())/255.0
  21 		#bgcolor = "#%02X%02X%02X" % (bgc.red(), bgc.green(), bgc.blue())
  22 
  23         self.fig = Figure(figsize=(width, height), dpi=dpi, facecolor=bgcolor, edgecolor=bgcolor)
  24         self.axes = self.fig.add_subplot(111)
  25         # We want the axes cleared every time plot() is called
  26         self.axes.hold(False)
  27 
  28         self.compute_initial_figure()
  29         
  30         FigureCanvas.__init__(self, self.fig)
  31         self.reparent(parent, QPoint(0, 0))
  32 
  33         FigureCanvas.setSizePolicy(self,
  34                                    QSizePolicy.Expanding,
  35                                    QSizePolicy.Expanding)
  36         FigureCanvas.updateGeometry(self)
  37 
  38     def sizeHint(self):
  39         w = self.fig.get_figwidth()
  40         h = self.fig.get_figheight()
  41         return QSize(w, h)
  42 
  43     def minimumSizeHint(self):
  44         return QSize(10, 10)
  45 
  46     def compute_initial_figure(self):
  47        	t = arange(0.0, 3.0, 0.01)
  48 	s = sin(2*pi*t)
  49 	self.axes.plot(t, s) 

New Attachment

File to upload
Rename to
Overwrite existing attachment of same name

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.