[SciPy-user] Why is SciPy better than Matlab or IDL? ;)

John Hunter jdh2358@gmail....
Sat Jul 11 06:41:56 CDT 2009


On Wed, Jul 8, 2009 at 2:35 PM, David Warde-Farley<dwf@cs.toronto.edu> wrote:
> On 8-Jul-09, at 7:48 AM, Tim Michelsen wrote:
>
>> Would be nice to see your implementation.
>
> The way I did it was to do the plotting onto a FigureCanvasAgg and
> then printing it to a fake file using cStringIO.
>
> from matplotlib.backends.backend_agg import FigureCanvasAgg
> import matplotlib.pyplot as plt
> import

Apologies since this is OT for scipy, so please followup to
matplolib-users if you have further questions or comments, but there
is a slight problem with the example above.  If you are using mpl in a
web server, and want explicit control of the canvas, you can either
use pyplot and the canvas like so::

  import matplotlib.use('Agg')
  import matplotlib.pyplot as plt
  fig = plt.figure()
  fig.canvas  # use thie canvas (it is FIgureCanvasAgg) rather than
creating your own

Or you can use the API and manually create your own figure and canvas
(this is recommended since you have maximum control over object
management)::

  from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
  from matplotlib.figure import Figure
  fig = Figure()
  canvas = FigureCanvas(fig)
  ax = fig.add_subplot(111)

The code you posted is a hybrid of using pyplot and managing your own
Canvas creation, which can work under certain configurations but is
brittle and not supported.  This is mainly because pyplot creates and
manages its own canvas instances.

JDH


More information about the SciPy-user mailing list