Matplotlib 是一个 Python 的 2D绘图库,它以各种硬拷贝格式和跨平台的交互式环境生成出版质量级别的图形。
有时候,我们想把matplotlib嵌入到wxpython,这样再利用pyinstaller打包便可形成一个脱离python编译器独立的画图工具。
下面是将matplotlib嵌入到wxpython的代码。
参考资料:https://stackoverflow.com/questions/10737459/embedding-a-matplotlib-figure-inside-a-wxpython-panel
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 |
# -*- coding: utf-8 -*- import wx import numpy as np import matplotlib # matplotlib采用WXAgg为后台,将matplotlib嵌入wxPython中 matplotlib.use("WXAgg") from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas from matplotlib.backends.backend_wxagg import NavigationToolbar2WxAgg as NavigationToolbar from matplotlib.ticker import MultipleLocator, FuncFormatter import pylab from matplotlib import pyplot ###################################################################################### class MPL_Panel_base(wx.Panel): ''''' #MPL_Panel_base面板,可以继承或者创建实例''' def __init__(self, parent): wx.Panel.__init__(self, parent=parent, id=-1) self.Figure = matplotlib.figure.Figure(figsize=(4, 3)) self.axes = self.Figure.add_axes([0.1, 0.1, 0.8, 0.8]) self.FigureCanvas = FigureCanvas(self, -1, self.Figure) self.NavigationToolbar = NavigationToolbar(self.FigureCanvas) self.StaticText = wx.StaticText(self, -1, label='Show Help String') self.SubBoxSizer = wx.BoxSizer(wx.HORIZONTAL) self.SubBoxSizer.Add(self.NavigationToolbar, proportion=0, border=2, flag=wx.ALL | wx.EXPAND) self.SubBoxSizer.Add(self.StaticText, proportion=-1, border=2, flag=wx.ALL | wx.EXPAND) self.TopBoxSizer = wx.BoxSizer(wx.VERTICAL) self.TopBoxSizer.Add(self.SubBoxSizer, proportion=-1, border=2, flag=wx.ALL | wx.EXPAND) self.TopBoxSizer.Add(self.FigureCanvas, proportion=-10, border=2, flag=wx.ALL | wx.EXPAND) self.SetSizer(self.TopBoxSizer) ###方便调用 self.pylab = pylab self.pl = pylab self.pyplot = pyplot self.numpy = np self.np = np self.plt = pyplot def UpdatePlot(self): '''''#修改图形的任何属性后都必须使用self.UpdatePlot()更新GUI界面 ''' self.FigureCanvas.draw() def plot(self, *args, **kwargs): '''''#最常用的绘图命令plot ''' self.axes.plot(*args, **kwargs) self.UpdatePlot() def semilogx(self, *args, **kwargs): ''''' #对数坐标绘图命令 ''' self.axes.semilogx(*args, **kwargs) self.UpdatePlot() def semilogy(self, *args, **kwargs): ''''' #对数坐标绘图命令 ''' self.axes.semilogy(*args, **kwargs) self.UpdatePlot() def loglog(self, *args, **kwargs): ''''' #对数坐标绘图命令 ''' self.axes.loglog(*args, **kwargs) self.UpdatePlot() def grid(self, flag=True): ''''' ##显示网格 ''' if flag: self.axes.grid() else: self.axes.grid(False) def title_MPL(self, TitleString="wxMatPlotLib Example In wxPython"): ''''' # 给图像添加一个标题 ''' self.axes.set_title(TitleString) def xlabel(self, XabelString="X"): ''''' # Add xlabel to the plotting ''' self.axes.set_xlabel(XabelString) def ylabel(self, YabelString="Y"): ''''' # Add ylabel to the plotting ''' self.axes.set_ylabel(YabelString) def xticker(self, major_ticker=1.0, minor_ticker=0.1): ''''' # 设置X轴的刻度大小 ''' self.axes.xaxis.set_major_locator(MultipleLocator(major_ticker)) self.axes.xaxis.set_minor_locator(MultipleLocator(minor_ticker)) def yticker(self, major_ticker=1.0, minor_ticker=0.1): ''''' # 设置Y轴的刻度大小 ''' self.axes.yaxis.set_major_locator(MultipleLocator(major_ticker)) self.axes.yaxis.set_minor_locator(MultipleLocator(minor_ticker)) def legend(self, *args, **kwargs): ''''' #图例legend for the plotting ''' self.axes.legend(*args, **kwargs) def xlim(self, x_min, x_max): ''' # 设置x轴的显示范围 ''' self.axes.set_xlim(x_min, x_max) def ylim(self, y_min, y_max): ''' # 设置y轴的显示范围 ''' self.axes.set_ylim(y_min, y_max) def savefig(self, *args, **kwargs): ''' #保存图形到文件 ''' self.Figure.savefig(*args, **kwargs) def cla(self): ''' # 再次画图前,必须调用该命令清空原来的图形 ''' self.axes.clear() self.Figure.set_canvas(self.FigureCanvas) self.UpdatePlot() def ShowHelpString(self, HelpString="Show Help String"): ''''' #可以用它来显示一些帮助信息,如鼠标位置等 ''' self.StaticText.SetLabel(HelpString) ################################################################ class MPL_Panel(MPL_Panel_base): ''''' #MPL_Panel重要面板,可以继承或者创建实例 ''' def __init__(self, parent): MPL_Panel_base.__init__(self, parent=parent) # 测试一下 self.FirstPlot() # 仅仅用于测试和初始化,意义不大 def FirstPlot(self): # self.rc('lines',lw=5,c='r') self.cla() x = np.arange(-5, 5, 0.25) y = np.sin(x) self.yticker(0.5, 0.1) self.xticker(1.0, 0.2) self.xlabel('X') self.ylabel('Y') self.title_MPL("图像") self.grid() self.plot(x, y, '--^g') ############################################################################### # MPL_Frame添加了MPL_Panel的1个实例 ############################################################################### class MPL_Frame(wx.Frame): """MPL_Frame可以继承,并可修改,或者直接使用""" def __init__(self, title="MPL_Frame Example In wxPython", size=(800, 500)): wx.Frame.__init__(self, parent=None, title=title, size=size) self.MPL = MPL_Panel_base(self) # 创建FlexGridSizer self.FlexGridSizer = wx.FlexGridSizer(rows=9, cols=1, vgap=5, hgap=5) self.FlexGridSizer.SetFlexibleDirection(wx.BOTH) self.RightPanel = wx.Panel(self, -1) # 测试按钮1 self.Button1 = wx.Button(self.RightPanel, -1, "刷新", size=(100, 40), pos=(10, 10)) self.Button1.Bind(wx.EVT_BUTTON, self.Button1Event) # 测试按钮2 self.Button2 = wx.Button(self.RightPanel, -1, "关于", size=(100, 40), pos=(10, 10)) self.Button2.Bind(wx.EVT_BUTTON, self.Button2Event) # 加入Sizer中 self.FlexGridSizer.Add(self.Button1, proportion=0, border=5, flag=wx.ALL | wx.EXPAND) self.FlexGridSizer.Add(self.Button2, proportion=0, border=5, flag=wx.ALL | wx.EXPAND) self.RightPanel.SetSizer(self.FlexGridSizer) self.BoxSizer = wx.BoxSizer(wx.HORIZONTAL) self.BoxSizer.Add(self.MPL, proportion=-10, border=2, flag=wx.ALL | wx.EXPAND) self.BoxSizer.Add(self.RightPanel, proportion=0, border=2, flag=wx.ALL | wx.EXPAND) self.SetSizer(self.BoxSizer) # 状态栏 self.StatusBar() # MPL_Frame界面居中显示 self.Centre(wx.BOTH) # 按钮事件,用于测试 def Button1Event(self, event): self.MPL.cla() # 必须清理图形,才能显示下一幅图 x = np.arange(-10, 10, 0.25) y = np.cos(x) self.MPL.plot(x, y, '--*g') self.MPL.xticker(2.0, 0.5) self.MPL.yticker(0.5, 0.1) self.MPL.title_MPL("MPL1") self.MPL.ShowHelpString("You Can Show MPL Helpful String Here !") self.MPL.grid() self.MPL.UpdatePlot() # 必须刷新才能显示 def Button2Event(self, event): self.AboutDialog() # 打开文件,用于测试 def DoOpenFile(self): wildcard = r"Data files (*.dat)|*.dat|Text files (*.txt)|*.txt|ALL Files (*.*)|*.*" open_dlg = wx.FileDialog(self, message='Choose a file', wildcard=wildcard, style='') if open_dlg.ShowModal() == wx.ID_OK: path = open_dlg.GetPath() try: file = open(path, 'r') text = file.read() file.close() except: dlg = wx.MessageDialog(self, 'Error opening file\n') dlg.ShowModal() open_dlg.Destroy() # 自动创建状态栏 def StatusBar(self): self.statusbar = self.CreateStatusBar() self.statusbar.SetFieldsCount(3) self.statusbar.SetStatusWidths([-2, -2, -1]) # About对话框 def AboutDialog(self): dlg = wx.MessageDialog(self, '\twxMatPlotLib\t\nMPL_Panel_base,MPL_Panel,MPL_Frame and MPL2_Frame \n Created by Wu Xuping\n Version 1.0.0 \n 2012-02-01', 'About MPL_Frame and MPL_Panel', wx.OK | wx.ICON_INFORMATION) dlg.ShowModal() dlg.Destroy() ############################################################################### ### MPL2_Frame添加了MPL_Panel的两个实例 ############################################################################### class MPL2_Frame(wx.Frame): """MPL2_Frame可以继承,并可修改,或者直接使用""" def __init__(self, title="MPL2_Frame Example In wxPython", size=(850, 500)): wx.Frame.__init__(self, parent=None, title=title, size=size) self.BoxSizer = wx.BoxSizer(wx.HORIZONTAL) self.MPL1 = MPL_Panel_base(self) self.BoxSizer.Add(self.MPL1, proportion=-1, border=2, flag=wx.ALL | wx.EXPAND) self.MPL2 = MPL_Panel_base(self) self.BoxSizer.Add(self.MPL2, proportion=-1, border=2, flag=wx.ALL | wx.EXPAND) self.RightPanel = wx.Panel(self, -1) self.BoxSizer.Add(self.RightPanel, proportion=0, border=2, flag=wx.ALL | wx.EXPAND) self.SetSizer(self.BoxSizer) # 创建FlexGridSizer self.FlexGridSizer = wx.FlexGridSizer(rows=9, cols=1, vgap=5, hgap=5) self.FlexGridSizer.SetFlexibleDirection(wx.BOTH) # 测试按钮1 self.Button1 = wx.Button(self.RightPanel, -1, "TestButton", size=(100, 40), pos=(10, 10)) self.Button1.Bind(wx.EVT_BUTTON, self.Button1Event) # 测试按钮2 self.Button2 = wx.Button(self.RightPanel, -1, "AboutButton", size=(100, 40), pos=(10, 10)) self.Button2.Bind(wx.EVT_BUTTON, self.Button2Event) # 加入Sizer中 self.FlexGridSizer.Add(self.Button1, proportion=0, border=5, flag=wx.ALL | wx.EXPAND) self.FlexGridSizer.Add(self.Button2, proportion=0, border=5, flag=wx.ALL | wx.EXPAND) self.RightPanel.SetSizer(self.FlexGridSizer) # 状态栏 self.StatusBar() # MPL2_Frame界面居中显示 self.Centre(wx.BOTH) # 按钮事件,用于测试 def Button1Event(self, event): self.MPL1.cla() # 必须清理图形,才能显示下一幅图 x = np.arange(-5, 5, 0.2) y = np.cos(x) self.MPL1.plot(x, y, '--*g') self.MPL1.xticker(2.0, 1.0) self.MPL1.yticker(0.5, 0.1) self.MPL1.title_MPL("MPL1") self.MPL1.ShowHelpString("You Can Show MPL1 Helpful String Here !") self.MPL1.grid() self.MPL1.UpdatePlot() # 必须刷新才能显示 self.MPL2.cla() self.MPL2.plot(x, np.sin(x), ':^b') self.MPL2.xticker(1.0, 0.5) self.MPL2.yticker(0.2, 0.1) self.MPL2.title_MPL("MPL2") self.MPL2.grid() self.MPL2.UpdatePlot() def Button2Event(self, event): self.AboutDialog() # 自动创建状态栏 def StatusBar(self): self.statusbar = self.CreateStatusBar() self.statusbar.SetFieldsCount(3) self.statusbar.SetStatusWidths([-2, -2, -1]) # About对话框 def AboutDialog(self): dlg = wx.MessageDialog(self, '\twxMatPlotLib\t\nMPL_Panel_base,MPL_Panel,MPL_Frame and MPL2_Frame \n Created by Wu Xuping\n Version 1.0.0 \n 2012-02-01', 'About MPL_Frame and MPL_Panel', wx.OK | wx.ICON_INFORMATION) dlg.ShowModal() dlg.Destroy() ######################################################################## # 主程序测试 if __name__ == '__main__': app = wx.App() frame = MPL2_Frame() #frame = MPL_Frame() frame.Center() frame.Show() app.MainLoop() |