【问题标题】:Matplotlib savefig only saves image, not the linesMatplotlib savefig 只保存图像,不保存线条
【发布时间】:2012-07-21 04:34:35
【问题描述】:

我使用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 后):

但我明白了:

这就是我正在做的保存图:

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 是我的模型,我可以从中获取所有图像信息。
  • 你保存为什么格式?
  • @pelson,我将照片保存为 PNG。
  • 您是否将艺术家添加到轴 ala axes.add_artist
  • @pelson,不,我不是;但是,这似乎并没有解决问题。谢谢你的建议。我在每个 draw_artist 调用下添加了 self.axes.add_artist(line)self.axes.add_artist(vertex)self.axes.add_artist(polyline.label)

标签: python matplotlib


【解决方案1】:

感谢 SSCCE。如果您禁用animated=True,那么一切正常。如果您需要 animated=True,可能值得询问 mpl 邮件列表,看看他们是否有任何进一步的见解。

【讨论】:

  • 感谢您的建议。我会在星期一的工作中试试这个,然后告诉你。
  • 您建议的更改对 Pelson 有效,谢谢。但是,我确实需要动画线条,所以我所做的只是在保存图像时设置animated=False,然后在保存后重置animated=True
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-18
  • 1970-01-01
  • 2018-03-07
  • 1970-01-01
  • 1970-01-01
  • 2021-05-25
相关资源
最近更新 更多