绘图中的空白后记文件
我在 wxmpl 面板中有一个 matplotlib 图,并且正在尝试将其打印到 Postscript 文件。我能得到的最好的结果是一个空白文件 - 我所说的“空白”并不是指空,而是一个带有标题和其他内容的 .ps 文件,但仅显示为空白页面。
要打印的代码是:
printer = self.GetPrinter() # wxmpl.FigurePrinter
fig = self.get_figure() # matplotlib.figure.Figure
printData = wx.PrintData(printer.getPrintData())
printer.printFigure(fig, "Plot")
self.printer.setPrintData(printData) # reset print data (avoid segfaults)
我查看了 wxmpl 代码,发现 OnPrintPage
从未被调用,因此我尝试通过 wxmpl.FigurePrintout.OnBeginDocument
添加对此的调用。然后 OnPrintPage
到达此行:
self.GetDC().DrawBitmap(image.ConvertToBitmap(), wM_Dx, hM_Dx, False)
并抛出错误:
wx._core.PyAssertionError
C++ assertion "wxAssertFailure" failed in ../src/generic/dcpsg.cpp(2238): invalid postscript dc
Google 引导我<一href="http://wxpython-users.1045709.n5.nabble.com/Fwd-wxPostscriptDC-problems-td2364034.html" rel="nofollow">此电子邮件字符串,因此我尝试替换上述内容line with:
dc = self.GetDC()
dc.StartDoc("printing stuff")
dc.StartPage()
dc.BeginDrawing()
dc.DrawBitmap(image.ConvertToBitmap(), wM_Dx, hM_Dx, False)
dc.EndDrawing()
dc.EndPage()
dc.EndDoc()
这消除了错误——但毕竟它仍然只是像以前一样打印空白文件。
另请注意,只需将绘图保存为 Postscript 即可正常工作 - 它使用不同的后端(FigureCanvasWxAgg 而不是 RendererAgg - 所以问题可能出在 matplotlib 而不是 wxmpl/wxPython 中?)。但是,我还需要将其打印到实际的打印机,因此 .ps 文件的特殊大小写并不是真正可行的解决方案。
运行 Linux
wxmpl版本1.2.9
wxPython版本2.6.4.0
matplotlib 版本 0.84 (是的,我知道它已经过时了,但这就是我现在必须使用的)
I have a matplotlib figure inside of a wxmpl panel, and am trying to print it to a Postscript file. The best I can get is a blank file - by 'blank' I don't mean empty, but rather a .ps file with headers and whatnot but displays as just a blank page.
The code to print is:
printer = self.GetPrinter() # wxmpl.FigurePrinter
fig = self.get_figure() # matplotlib.figure.Figure
printData = wx.PrintData(printer.getPrintData())
printer.printFigure(fig, "Plot")
self.printer.setPrintData(printData) # reset print data (avoid segfaults)
I took a look into the wxmpl code and found that OnPrintPage
is never called, so I tried adding a call to that via wxmpl.FigurePrintout.OnBeginDocument
. Then OnPrintPage
gets to this line:
self.GetDC().DrawBitmap(image.ConvertToBitmap(), wM_Dx, hM_Dx, False)
and throws an error:
wx._core.PyAssertionError
C++ assertion "wxAssertFailure" failed in ../src/generic/dcpsg.cpp(2238): invalid postscript dc
Google led me to this email string, and accordingly I tried replacing the above line with:
dc = self.GetDC()
dc.StartDoc("printing stuff")
dc.StartPage()
dc.BeginDrawing()
dc.DrawBitmap(image.ConvertToBitmap(), wM_Dx, hM_Dx, False)
dc.EndDrawing()
dc.EndPage()
dc.EndDoc()
That got rid of the error -- but after all that it's still just printing blank files as before.
Another note, simply saving the plot as Postscript works fine - it uses a different backend (FigureCanvasWxAgg instead of RendererAgg - so maybe the problem is in matplotlib instead of wxmpl/wxPython?). However, I need to print this to an actual printer as well, so special-casing for .ps files isn't really a viable solution.
Running Linux
wxmpl version 1.2.9
wxPython version 2.6.4.0
matplotlib version 0.84 (yes, I know it's horribly outdated, but that's what I have to work with for now)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是 wxmpl 没有调用
HasPage
——毕竟,上面的全部更改并不重要。在类FigurePrintout
中添加此代码修复了不打印的问题:是的...两行。根据 wxPython 打印框架的文档,默认值是
return page == 1
,但 wxmpl 在覆盖GetPageInfo
时返回 0 作为第一页。因此打印机认为没有页面可以打印。The problem was that wxmpl had no call to
HasPage
-- the entirety of the changes above didn't matter, after all. Adding in this code in classFigurePrintout
fixed the issue with not printing:Yep... two lines. Based on the documentation for the wxPython printing framework, the default is
return page == 1
, but wxmpl returns 0 as the first page in it's override ofGetPageInfo
. So the printer didn't think it had a page to print.