1 | # -*- coding: utf-8 -*- |
---|
2 | #GSASIItestplot.py |
---|
3 | ''' |
---|
4 | *GSASIItestplot: Plotting for testDeriv* |
---|
5 | ======================================== |
---|
6 | |
---|
7 | Plotting module used for script testDeriv. |
---|
8 | ''' |
---|
9 | import wx |
---|
10 | import wx.aui |
---|
11 | import matplotlib as mpl |
---|
12 | try: |
---|
13 | from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as Canvas |
---|
14 | except ImportError: |
---|
15 | from matplotlib.backends.backend_wx import FigureCanvas as Canvas |
---|
16 | try: |
---|
17 | from matplotlib.backends.backend_wxagg import NavigationToolbar2WxAgg as Toolbar |
---|
18 | except ImportError: |
---|
19 | from matplotlib.backends.backend_wxagg import Toolbar as Toolbar # name changes in wx4.0.1 |
---|
20 | |
---|
21 | class Plot(wx.Panel): |
---|
22 | 'Creates a plotting window' |
---|
23 | def __init__(self, parent, id = -1, dpi = None, **kwargs): |
---|
24 | wx.Panel.__init__(self, parent, id=id, **kwargs) |
---|
25 | self.figure = mpl.figure.Figure(dpi=dpi, #figsize=(5,7) |
---|
26 | ) |
---|
27 | self.canvas = Canvas(self, -1, self.figure) |
---|
28 | self.toolbar = Toolbar(self.canvas) |
---|
29 | self.toolbar.Realize() |
---|
30 | |
---|
31 | sizer = wx.BoxSizer(wx.VERTICAL) |
---|
32 | sizer.Add(self.canvas,1,wx.EXPAND) |
---|
33 | sizer.Add(self.toolbar, 0 , wx.LEFT | wx.EXPAND) |
---|
34 | self.SetSizer(sizer) |
---|
35 | |
---|
36 | class PlotNotebook(wx.Panel): |
---|
37 | 'creates a Wx application and a plotting notebook' |
---|
38 | def __init__(self, id = -1): |
---|
39 | self.app = wx.App() |
---|
40 | self.frame = wx.Frame(None,-1,'Plotter', size=wx.Size(600,600), |
---|
41 | style=wx.DEFAULT_FRAME_STYLE ^ wx.CLOSE_BOX) |
---|
42 | self.status = self.frame.CreateStatusBar() |
---|
43 | self.status.SetStatusText('Use K-box to set plot controls') |
---|
44 | wx.Panel.__init__(self, self.frame, id=id) |
---|
45 | self.nb = wx.aui.AuiNotebook(self, |
---|
46 | style=wx.aui.AUI_NB_DEFAULT_STYLE ^ wx.aui.AUI_NB_CLOSE_ON_ACTIVE_TAB) |
---|
47 | sizer = wx.BoxSizer() |
---|
48 | sizer.Add(self.nb, 1, wx.EXPAND) |
---|
49 | self.SetSizer(sizer) |
---|
50 | |
---|
51 | def Show(self): |
---|
52 | self.frame.Show() |
---|
53 | |
---|
54 | def StartEventLoop(self): |
---|
55 | self.Show() |
---|
56 | self.app.MainLoop() |
---|
57 | |
---|
58 | def add(self,name="plot"): |
---|
59 | |
---|
60 | def OnMotion(event): |
---|
61 | xpos = event.xdata |
---|
62 | if xpos: #avoid out of frame mouse position |
---|
63 | ypos = event.ydata |
---|
64 | self.status.SetStatusText('X= %.3f Y= %.3f'%(xpos,ypos)) |
---|
65 | |
---|
66 | page = Plot(self.nb) |
---|
67 | page.canvas.mpl_connect('motion_notify_event', OnMotion) |
---|
68 | self.nb.AddPage(page,name) |
---|
69 | return page.figure |
---|