Matplotlib savefig只保存图像,而不是行

时间:2012-07-19 20:39:47

标签: python matplotlib

我有一个使用figure.savefig(path)保存的matplotlib图。这样可以保存我在画布上的图像,但是它不会保存我在图像上绘制的线条。

以下是我创建数据的方式:

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

现在,我在画布上画画(使用draw_artist方法):

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)

这在程序中很有用(在图像上显示线条)但是当我尝试使用savefig()时,只保存图像而不是线条。

图片应如下所示(保存到PNG后):

image with lines

但我得到了这个:

after saving (without lines)

这就是我要保存的数字:

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

任何想法为什么这可能不会保存我正在绘制的线条,而只是保存正在绘制线条的图像?

谢谢。

编辑:这是一个SSCCE:http://pastebin.com/VQG165k0(只需更改您要加载的保存位置和图像)。

1 个答案:

答案 0 :(得分:1)

感谢SSCCE。如果你禁用animated=True那么事情就可以了。如果你需要animated = True,可能值得问一下mpl邮件列表,看看他们是否有进一步的见解。