#!/usr/bin/env python """ An example of how to use wx or wxagg in an application w. or w/o the toolbar """ import matplotlib matplotlib.use('WXAgg') from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas from matplotlib.backends.backend_wx import NavigationToolbar2Wx from matplotlib.figure import Figure from matplotlib.numerix import arange, sin, pi from wxPython.wx import * import matplotlib.matlab as mpl import wx import os # Modify this to the full path of the PNG file in which the graph will be saved IMAGE_FILE = 'd:\\temp\\graph.png' # Modify this to set printing resolution PRINT_DPI = 150 # Class derived from wx.Printout, example adapted from the wxDemo class MyPrintout(wx.Printout): def __init__(self, canvas, w, h ): """ Class initiazation Input: canvas - class that has the DoDrawing function (used below in OnPrintPage) Input: w - width of image that must be printed (in pixels) Input: h - height of image that must be printed (in pixels) """ wx.Printout.__init__(self) self.cw = w self.ch = h self.canvas = canvas def HasPage(self, page): if page <= 1: return True else: return False def GetPageInfo(self): return (1, 1, 1, 1) def OnPrintPage(self, page): dc = self.GetDC() #------------------------------------------- # One possible method of setting scaling factors... maxX = self.cw maxY = self.ch # Let's have at least 5 device units margin marginX = 5 marginY = 5 # Add the margin to the graphic size maxX = maxX + (2 * marginX) maxY = maxY + (2 * marginY) # Get the size of the DC in pixels (w, h) = dc.GetSizeTuple() # Calculate a suitable scaling factor scaleX = float(w) / maxX scaleY = float(h) / maxY # Use x or y scaling factor, whichever fits on the DC actualScale = min(scaleX, scaleY) # Calculate the position on the DC for centering the graphic posX = (w - (self.cw * actualScale)) / 2.0 posY = (h - (self.ch * actualScale)) / 2.0 # Set the scale and origin dc.SetUserScale(actualScale, actualScale) dc.SetDeviceOrigin(int(posX), int(posY)) self.canvas.DoDrawing(dc) return True # A cursor class, for graph interaction class Cursor: def __init__(self, canvas, ax): self.canvas = canvas self.ax = ax def mouse_move(self, event): x, y = event.x, event.y if event.inaxes: ax = event.inaxes minx, maxx = ax.get_xlim() miny, maxy = ax.get_ylim() print 'x=%1.2f, y=%1.2f'%(event.xdata, event.ydata) # Declare menu IDs ID_Preview = wx.NewId() ID_Print = wx.NewId() class CanvasFrame(wxFrame): def __init__(self): wxFrame.__init__( self, None, -1, 'CanvasFrame',size=( 900,900 ) ) self.SetBackgroundColour(wxNamedColor("WHITE")) self.figure = Figure() self.axes = self.figure.add_axes( [ 0.04, 0.04, 0.92, 0.92 ] ) t = arange(0.0,3.0,0.01) s = sin(2*pi*t) c = sin(4*pi*t) p = self.axes.fill(t,s,'b',t,c,'g' ) p[ 0 ].set_alpha( 0.2 ) p[ 1 ].set_alpha( 0.2 ) self.axes.plot(t,c,'g') self.axes.vlines( [1.5], -1.0, 1.0 ) self.canvas = FigureCanvas(self, -1, self.figure) self.sizer = wxBoxSizer(wxVERTICAL) self.sizer.Add(self.canvas, 1, wxTOP | wxLEFT | wxEXPAND) self.SetSizer(self.sizer) self.SetAutoLayout( True ) self.sizer.Fit( self ) cursor = Cursor(self.canvas, self.axes) self.canvas.mpl_connect('motion_notify_event', cursor.mouse_move) self.add_toolbar() # comment this out for no toolbar # Capture the paint message #EVT_PAINT(self, self.OnPaint) #EVT_SIZE( self, self.OnSize) # Setup print data # Modify this to change default print parameters (paper type and orientation) self.printData = wx.PrintData() self.printData.SetPaperId(wx.PAPER_A4) self.printData.SetPrintMode(wx.PRINT_MODE_PRINTER) self.printData.SetOrientation(wx.LANDSCAPE) # Add a Menu Bar menuBar = wxMenuBar() self.menuFile = wxMenu() self.menuFile.Append(ID_Preview, "Preview", "Preview print") EVT_MENU(self, ID_Preview, self.OnPreview) self.menuFile.Append(ID_Print, "Print", "Print") EVT_MENU(self, ID_Print, self.OnPrint) menuBar.Append(self.menuFile, '&File') self.SetMenuBar(menuBar) def DoDrawing( self, dc ): dc.DrawBitmap( self.bmp, 0, 0, False ) def OnPreview( self, evt ): # Save the current plot as a file self.canvas.print_figure( IMAGE_FILE, dpi = PRINT_DPI, orientation = 'landscape' ) # Save the current plot as a file timg = wx.Image( IMAGE_FILE, wx.BITMAP_TYPE_PNG) self.bmp = wx.BitmapFromImage( timg ) data = wx.PrintDialogData( self.printData ) printout = MyPrintout( self, self.bmp.GetWidth(), self.bmp.GetHeight() ) printout2 = MyPrintout( self, self.bmp.GetWidth(), self.bmp.GetHeight() ) self.preview = wx.PrintPreview(printout, printout2, data) if not self.preview.Ok(): print "Preview problem" return frame = wx.PreviewFrame(self.preview, self, "This is a print preview") frame.Initialize() frame.SetPosition(self.GetPosition()) frame.SetSize(self.GetSize()) frame.Show(True) def OnPrint(self, event): # Save the current plot as a file self.canvas.print_figure( IMAGE_FILE, dpi = PRINT_DPI, orientation = 'landscape' ) # Save the current plot as a file timg = wx.Image( IMAGE_FILE, wx.BITMAP_TYPE_PNG) self.bmp = wx.BitmapFromImage( timg ) pdd = wx.PrintDialogData(self.printData) printer = wx.Printer(pdd) printout = MyPrintout( self, self.bmp.GetWidth(), self.bmp.GetHeight() ) if not printer.Print(self, printout, True): wx.MessageBox("There was a problem printing.\nPerhaps your current printer is not set correctly?", "Printing", wx.OK) else: self.printData = wx.PrintData( printer.GetPrintDialogData().GetPrintData() ) printout.Destroy() def add_toolbar(self): self.toolbar = NavigationToolbar2Wx(self.canvas) self.toolbar.Realize() if wxPlatform == '__WXMAC__': # Mac platform (OSX 10.3, MacPython) does not seem to cope with # having a toolbar in a sizer. This work-around gets the buttons # back, but at the expense of having the toolbar at the top self.SetToolBar(self.toolbar) else: # On Windows platform, default window size is incorrect, so set # toolbar width to figure width. tw, th = self.toolbar.GetSizeTuple() fw, fh = self.canvas.GetSizeTuple() # By adding toolbar in sizer, we are able to put it at the bottom # of the frame - so appearance is closer to GTK version. # As noted above, doesn't work for Mac. self.toolbar.SetSize(wxSize(fw, th)) self.sizer.Add(self.toolbar, 0, wxLEFT | wxEXPAND) # update the axes menu on the toolbar self.toolbar.update() def OnSize( self, event ): print "OnSize" print event.GetSize() event.Skip() class App(wxApp): def OnInit(self): 'Create the main window and insert the custom frame' frame = CanvasFrame() frame.Show(true) return true app = App(0) app.MainLoop()

AltStyle によって変換されたページ (->オリジナル) /