Skip to main content
 首页 » 编程设计

python之将 matplotlib 图放入 wxpython

2025年05月04日9JustinYoung

我有创建交互式散点图的代码:

if 1: # picking on a scatter plot (matplotlib.collections.RegularPolyCollection) 
 
x, y, c, s = rand(4, 100) 
def onpick3(event): 
    ind = event.ind 
    print('onpick3 scatter:', ind, np.take(x, ind), np.take(y, ind)) 
 
fig = figure() 
ax1 = fig.add_subplot(111) 
col = ax1.scatter(x, y, 100*s, c, picker=True) 
#fig.savefig('pscoll.eps') 
fig.canvas.mpl_connect('pick_event', onpick3) 

我想做的是将这个图集成到wxPython中

我该怎么做呢?

请您参考如下方法:

您要做的是将图形放入 wxPanel 中。参见 here for a good example .

class CanvasPanel(wx.Panel): 
    def __init__(self, parent): 
        wx.Panel.__init__(self, parent) 
 
        self.figure = Figure() 
        self.axes1 = self.figure.add_subplot(211) 
        self.axes2 = self.figure.add_subplot(212) 
        self.canvas = FigureCanvas(self, -1, self.figure) 
        self.sizer.Add(self.canvas, 1, wx.LEFT | wx.TOP | wx.GROW) 
        self.SetSizer(self.sizer) 
        self.Fit() 
        self.figure.tight_layout() 
        self.canvas.mpl_connect('motion_notify_event', self.onMotion) 
 
    def onMotion(self, event): 
        if event.inaxes: 
            xpos = event.xdata 
            print(' %0.1f' % (xpos))