Matplotlib的savefig只保存图像,而不是线条

4
我有一幅使用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(只需更改保存位置和要加载的图像)。

@nye17,这是一个扩展wx.Frame的类:class View(wx.Frame):,在我的另一个类中,我将其实例化为:self.view = dicom_view.View(self, self.model),其中self是我的控制器,self.model是我的模型,我可以从中获取所有图像信息。 - adchilds
你保存的格式是什么? - pelson
@pelson,我正在将照片保存为PNG格式。 - adchilds
你是否正在通过 axes.add_artist 将艺术家添加到轴上? - pelson
@pelson,不是的;然而,那似乎并没有解决问题。感谢您的建议。我在每个draw_artist调用下添加了self.axes.add_artist(line)self.axes.add_artist(vertex)以及self.axes.add_artist(polyline.label) - adchilds
实际上,因为你正在使用 axes.plot,所以不需要使用 add_artist - pelson
1个回答

1

感谢提供 SSCCE。如果您禁用 animated=True,则事情会正常运行。如果您需要 animated=True,则值得询问 mpl 邮件列表,以查看他们是否有更深入的见解。


谢谢你的建议。我会在周一尝试这个方法,并告诉你结果。 - adchilds
你的建议修改起作用了,Pelson,谢谢。然而,我确实需要这些线条进行动画处理,所以我所做的就是在保存图像时设置animated=False,然后在保存后重新设置为animated=True - adchilds

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接