adchilds
adchilds

Reputation: 963

Matplotlib savefig only saves image, not the lines

I have a matplotlib figure that I'm saving, using figure.savefig(path). This saves the image that I have on my canvas, within the figure, but it doesn't save the lines that I have drawn over the image.

Here's how I'm creating my figure:

if new:
    y, x = self.model.get_image_shape()
    self.figure = Figure(figsize=(x*2/72.0, y*2/72.0), dpi=72)
    self.canvas = FigureCanvasWxAgg(self.scroll, -1, self.figure)
    self.canvas.SetBackgroundColour('grey')
self.axes = self.figure.add_axes([0.0, 0.0, 1.0, 1.0])
self.axes.set_axis_off()
self.axes.imshow(self.model.get_image(), aspect='auto')
self.axes.set_autoscale_on(False)
self.mpl_bindings()
y, = self.scroll.GetSizeTuple()[-1:]
iHt, = self.model.get_image_shape()[:-1]
self.aspect = (float(y)/float(iHt)) # zoom factor (0.0 - 1.0)
self.controller.resize_image() # Resizes our figure according to the zoom factor

Now, I draw on the canvas as such (using the draw_artist method):

def draw_polylines(self, adjustable, locked):
    if self.tmp_line: self.axes.draw_artist(self.tmp_line)
    for polyline in self.polylines:
        for line in polyline.lines:
            self.axes.draw_artist(line)
        if adjustable:
            for vertex in polyline.verticies:
                self.axes.draw_artist(vertex)
        self.axes.draw_artist(polyline.label)

This works all good in the program (displaying the lines over the image) but when I try to use savefig(), only the image saves, not the lines.

Image should look like this (after saving to PNG):

image with lines

But I'm getting this:

after saving (without lines)

This is what I'm doing to save the figure:

self.view.figure.savefig(dialog.GetPath(), dpi=self.view.figure.dpi)

Any ideas why this may not be saving the lines that I'm drawing, but only the image that the lines are being drawn on?

Thank you.

EDIT: Here's a SSCCE: http://pastebin.com/VQG165k0 (just change the save location and image that you'll be loading).

Upvotes: 4

Views: 641

Answers (1)

pelson
pelson

Reputation: 21829

Thanks for the SSCCE. If you disable animated=True then things work. If you need animated=True, it might be worth asking the mpl mailing list to see if they have any further insight.

Upvotes: 1

Related Questions