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.setPri开发者_Python百科ntData(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)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 class FigurePrintout
fixed the issue with not printing:
def HasPage(self, page):
return page <= 1
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 of GetPageInfo
. So the printer didn't think it had a page to print.
精彩评论