0
我有一個簡單的wxFrame和兩個面板。我想要其中一個面板顯示matplotlib條形圖。我學會了使用圖表,但我使用的show()函數在新窗口中爲我提供了圖表。Matplotlib條形圖中的wx框架而不是新窗口
import wx
import wx.grid as gridlib
import os
import Image
import pylab as p
class PanelOne(wx.Panel):
def __init__(self, parent):
"""Constructor"""
wx.Panel.__init__(self, parent=parent)
#txt = wx.TextCtrl(self)
button =wx.Button(self, label="Save", pos=(200, 325))
button.Bind(wx.EVT_BUTTON, parent.onSwitchPanels)
class PanelTwo(wx.Panel):
def __init__(self, parent):
"""Constructor"""
wx.Panel.__init__(self, parent=parent)
sizer = wx.BoxSizer(wx.VERTICAL)
self.SetSizer(sizer)
fig=p.figure()
ax=fig.add_subplot(1,1,1)
x=[1,2,3]
y=[4,6,3]
ax.bar(x,y)
p.show()
class MyForm(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY,
"Panel Switch",
size=(800,600))
self.panel_one = PanelOne(self)
self.panel_two = PanelTwo(self)
self.panel_two.Hide()
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.sizer.Add(self.panel_one, 1, wx.EXPAND)
self.sizer.Add(self.panel_two, 1, wx.EXPAND)
self.SetSizer(self.sizer)
menubar = wx.MenuBar()
fileMenu = wx.Menu()
switch_panels_menu_item = fileMenu.Append(wx.ID_ANY,
"Switch Panels",
"Some text")
self.Bind(wx.EVT_MENU, self.onSwitchPanels,
switch_panels_menu_item)
menubar.Append(fileMenu, '&File')
self.SetMenuBar(menubar)
-------------------------------------------------------------------
def onSwitchPanels(self, event):
if self.panel_one.IsShown():
self.SetTitle("Panel Two Showing")
self.panel_one.Hide()
self.panel_two.Show()
else:
self.SetTitle("Panel One Showing")
self.panel_one.Show()
self.panel_two.Hide()
self.Layout()
if __name__ == "__main__":
app = wx.App(False)
frame = MyForm()
frame.Show()
app.MainLoop()
會發生什麼事,我得到了一個新的matplotlib窗口中的圖表,當我關閉它,我的框架出現。我需要將圖表放在框架的第二個面板中。
我試過讀This,但沒有完全幫助我。
此作品在框架上。但我正在尋找將圖表嵌入其中一個面板中。和條形圖。我在面板中試過你的代碼,但失敗了。由於無法顯示條形圖,我使用了pylot。 – 2013-02-26 16:07:20
我無法對'失敗'做任何事情。你應該用_exactly_來編輯你的問題,或者你嘗試過或打開一個新的問題。要獲得條形圖而不是線條,只需更改'self.axes.plot(...)' - >'self.axes.bar(...)'。 – tacaswell 2013-02-26 16:41:11
其實我調整了一下,並改變了情節,像你說的吧,它工作正常。謝謝。 – 2013-02-26 16:51:20