OpenMPCD
CanvasFrame.py
1 from .PlotSelectionFrame import PlotSelectionFrame
2 
3 import matplotlib.backends.backend_wx
4 import matplotlib.backends.backend_wxagg
5 import matplotlib.figure
6 import wx
7 
8 class CanvasFrame(wx.Frame):
9  """
10  Main window, containing the plotting canvas.
11  """
12 
13  def __init__(self, plotter):
14  sizeX = 1200
15  sizeY = 780
16  dpi = 100
17  wx.Frame.__init__(self, None, title='my title', size=(sizeX, sizeY))
18 
19  self.plotter = plotter
20 
21  self.mainSizer = wx.BoxSizer(wx.VERTICAL)
22  self.SetSizer(self.mainSizer)
23 
24  self.canvasHorizontalSizer = wx.BoxSizer(wx.HORIZONTAL)
25  self.mainSizer.Add(self.canvasHorizontalSizer, 1, wx.LEFT | wx.TOP | wx.EXPAND)
26 
27  self.figure = matplotlib.figure.Figure(figsize=(sizeX / dpi, sizeY / dpi), dpi=dpi)
28  self.canvas = matplotlib.backends.backend_wxagg.FigureCanvasWxAgg(self, wx.ID_ANY, self.figure)
29  self.canvasHorizontalSizer.Add(self.canvas, 1, wx.LEFT | wx.TOP | wx.EXPAND)
30 
31 
32 
33  self.plotOptions = {}
34 
35  self.plotOptions['sizer'] = wx.BoxSizer(wx.HORIZONTAL)
36  self.mainSizer.Add(self.plotOptions['sizer'], 0, wx.LEFT | wx.EXPAND)
37 
38 
39  self.plotOptions['checkBoxes'] = {}
40 
41  self.plotOptions['checkBoxes']['logx'] = wx.CheckBox(self)
42  self.plotOptions['sizer'].Add(self.plotOptions['checkBoxes']['logx'])
43  self.Bind(wx.EVT_CHECKBOX, self.onPlotOptionsCheckbox, self.plotOptions['checkBoxes']['logx'])
44  self.plotOptions['sizer'].Add(wx.StaticText(self, label="log-x"), 0, wx.LEFT | wx.EXPAND)
45 
46  self.plotOptions['checkBoxes']['logy'] = wx.CheckBox(self)
47  self.plotOptions['sizer'].Add(self.plotOptions['checkBoxes']['logy'])
48  self.Bind(wx.EVT_CHECKBOX, self.onPlotOptionsCheckbox, self.plotOptions['checkBoxes']['logy'])
49  self.plotOptions['sizer'].Add(wx.StaticText(self, label="log-y"), 0, wx.LEFT | wx.EXPAND)
50 
51  self.plotOptions['checkBoxes']['dashedIfNegative'] = wx.CheckBox(self)
52  self.plotOptions['sizer'].Add(self.plotOptions['checkBoxes']['dashedIfNegative'])
53  self.Bind(wx.EVT_CHECKBOX, self.onPlotOptionsCheckbox, self.plotOptions['checkBoxes']['dashedIfNegative'])
54  self.plotOptions['sizer'].Add(wx.StaticText(self, label="dashed-if-negative"), 0, wx.LEFT | wx.EXPAND)
55 
56 
57  self.toolbar = matplotlib.backends.backend_wx.NavigationToolbar2Wx(self.canvas)
58  self.toolbar.Realize()
59  self.toolbar.Show()
60  self.mainSizer.Add(self.toolbar, 0, wx.LEFT | wx.EXPAND)
61 
62  self._statusBar = wx.StatusBar(self.canvas)
63  self._statusBar.SetFieldsCount(1)
64  self.SetStatusBar(self._statusBar)
65  self.canvas.mpl_connect('motion_notify_event', self._updateStatusBar)
66 
67 
68 
69  self.Fit()
70 
71 
72  self.axes = self.figure.add_subplot(1, 1, 1)
73 
74 
75  self.plotSelectionFrame = PlotSelectionFrame(self, plotter)
76  self.plotSelectionFrame.Show(True)
77 
78  def update(self):
79  self.updateLegend()
80  self.updateCanvas()
81 
82  def clear(self):
83  self.axes.cla()
84 
85  def updateCanvas(self):
86  self.canvas.draw()
87 
88  def updateLegend(self):
89  #box = self.axes.get_position()
90  #self.axes.set_position([box.x0, box.y0, box.width * 0.9, box.height])
91  handlesAndLegends = self.axes.get_legend_handles_labels()
92  if len(handlesAndLegends[0]) != 0:
93  self.legend = self.axes.legend(loc='center left', bbox_to_anchor=(1, 0.5))
94 
95 
96  def onPlotOptionsCheckbox(self, event):
97  if event.GetId() == self.plotOptions['checkBoxes']['logx'].GetId():
98  if event.IsChecked():
99  self.plotter.setXScale('log')
100  else:
101  self.plotter.setXScale('linear')
102  elif event.GetId() == self.plotOptions['checkBoxes']['logy'].GetId():
103  if event.IsChecked():
104  self.plotter.setYScale('log')
105  else:
106  self.plotter.setYScale('linear')
107  elif event.GetId() == self.plotOptions['checkBoxes']['dashedIfNegative'].GetId():
108  if event.IsChecked():
109  self.plotter.setPlotDashedIfNegative(True)
110  else:
111  self.plotter.setPlotDashedIfNegative(False)
112  else:
113  raise ValueError("Cannot handle event")
114 
115  self.update()
116 
117  def setLogXCheckbox(self, value):
118  """
119  Sets the checkbox that signifies that the x-axis is logarithmic.
120 
121  @param[in] value True to activate the checkbox, false to deactivate.
122  """
123 
124  self.plotOptions['checkBoxes']['logx'].SetValue(value)
125 
126  def setLogYCheckbox(self, value):
127  """
128  Sets the checkbox that signifies that the y-axis is logarithmic.
129 
130  @param[in] value True to activate the checkbox, false to deactivate.
131  """
132 
133  self.plotOptions['checkBoxes']['logy'].SetValue(value)
134 
135  def setDashedIfNegativeCheckbox(self, value):
136  """
137  Sets the checkbox that controls the plot-dashed-if-negative feature.
138 
139  @param[in] value True to activate the checkbox, false to deactivate.
140  """
141 
142  self.plotOptions['checkBoxes']['dashedIfNegative'].SetValue(value)
143 
144  def _updateStatusBar(self, event):
145  """
146  Updates the status bar after an event happened.
147 
148  @param[in] event The event to react to.
149  """
150 
151  if event.inaxes:
152  x, y = event.xdata, event.ydata
153  self._statusBar.SetStatusText('x = ' + str(x) + ', y = ' + str(y))
MPCDAnalysis.InteractivePlotter.CanvasFrame.CanvasFrame.setDashedIfNegativeCheckbox
def setDashedIfNegativeCheckbox(self, value)
Definition: CanvasFrame.py:144
MPCDAnalysis.InteractivePlotter.CanvasFrame.CanvasFrame.updateLegend
def updateLegend(self)
Definition: CanvasFrame.py:89
MPCDAnalysis.InteractivePlotter.CanvasFrame.CanvasFrame.plotOptions
plotOptions
Definition: CanvasFrame.py:34
MPCDAnalysis.InteractivePlotter.CanvasFrame.CanvasFrame.mainSizer
mainSizer
Definition: CanvasFrame.py:22
MPCDAnalysis.InteractivePlotter.CanvasFrame.CanvasFrame.onPlotOptionsCheckbox
def onPlotOptionsCheckbox(self, event)
Definition: CanvasFrame.py:97
MPCDAnalysis.InteractivePlotter.CanvasFrame.CanvasFrame.updateCanvas
def updateCanvas(self)
Definition: CanvasFrame.py:86
MPCDAnalysis.InteractivePlotter.PlotSelectionFrame.PlotSelectionFrame
Definition: PlotSelectionFrame.py:9
MPCDAnalysis.InteractivePlotter.CanvasFrame.CanvasFrame.toolbar
toolbar
Definition: CanvasFrame.py:58
MPCDAnalysis.InteractivePlotter.CanvasFrame.CanvasFrame.legend
legend
Definition: CanvasFrame.py:94
MPCDAnalysis.InteractivePlotter.CanvasFrame.CanvasFrame.setLogXCheckbox
def setLogXCheckbox(self, value)
Definition: CanvasFrame.py:124
MPCDAnalysis.InteractivePlotter.CanvasFrame.CanvasFrame.plotSelectionFrame
plotSelectionFrame
Definition: CanvasFrame.py:76
MPCDAnalysis.InteractivePlotter.CanvasFrame.CanvasFrame.update
def update(self)
Definition: CanvasFrame.py:79
MPCDAnalysis.InteractivePlotter.CanvasFrame.CanvasFrame.axes
axes
Definition: CanvasFrame.py:73
MPCDAnalysis.InteractivePlotter.CanvasFrame.CanvasFrame.canvasHorizontalSizer
canvasHorizontalSizer
Definition: CanvasFrame.py:25
MPCDAnalysis.InteractivePlotter.CanvasFrame.CanvasFrame.plotter
plotter
Definition: CanvasFrame.py:20
MPCDAnalysis.InteractivePlotter.CanvasFrame.CanvasFrame._updateStatusBar
def _updateStatusBar(self, event)
Definition: CanvasFrame.py:154
MPCDAnalysis.InteractivePlotter.CanvasFrame.CanvasFrame._statusBar
_statusBar
Definition: CanvasFrame.py:63
MPCDAnalysis.InteractivePlotter.CanvasFrame.CanvasFrame.setLogYCheckbox
def setLogYCheckbox(self, value)
Definition: CanvasFrame.py:134
MPCDAnalysis.InteractivePlotter.CanvasFrame.CanvasFrame.figure
figure
Definition: CanvasFrame.py:28
MPCDAnalysis.InteractivePlotter.CanvasFrame.CanvasFrame.canvas
canvas
Definition: CanvasFrame.py:29