Skip to content Skip to sidebar Skip to footer

Matplotlib Bar Chart In A Wx Frame Instead Of A New Window

I have a simple wxFrame and two panels in it. i want one of the panels to show a matplotlib bar chart. I learnt to use the chart, but the show() function that I use gives me the ch

Solution 1:

The pyplot functions you are using (p.figure) have the primary purpose of taking care of all the GUI details (which works at cross purposes to what you want). What you want to to is embed matplotlib in your pre-existing gui. For how to do this, see these examples.

This code is pulled from embedding_in_wx2.py to protect against link-rot:

#!/usr/bin/env python"""
An example of how to use wx or wxagg in an application with the new
toolbar - comment out the setA_toolbar line for no toolbar
"""# Used to guarantee to use at least Wx2.8import wxversion
wxversion.ensureMinimal('2.8')
from numpy import arange, sin, pi

import matplotlib

# uncomment the following to use wx rather than wxagg#matplotlib.use('WX')#from matplotlib.backends.backend_wx import FigureCanvasWx as FigureCanvas# comment out the following to use wx rather than wxagg
matplotlib.use('WXAgg')
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas

from matplotlib.backends.backend_wx import NavigationToolbar2Wx

from matplotlib.figure import Figure
import wx

classCanvasFrame(wx.Frame):

    def__init__(self):
        wx.Frame.__init__(self,None,-1,
                         'CanvasFrame',size=(550,350))

        self.SetBackgroundColour(wx.NamedColour("WHITE"))
        self.figure = Figure()
        self.axes = self.figure.add_subplot(111)
        t = arange(0.0,3.0,0.01)
        s = sin(2*pi*t)
        self.axes.plot(t,s)
        self.canvas = FigureCanvas(self, -1, self.figure)
        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.sizer.Add(self.canvas, 1, wx.LEFT | wx.TOP | wx.GROW)
        self.SetSizer(self.sizer)
        self.Fit()

    defOnPaint(self, event):
        self.canvas.draw()

classApp(wx.App):
    defOnInit(self):
        'Create the main window and insert the custom frame'
        frame = CanvasFrame()
        frame.Show(True)
        returnTrue

app = App(0)
app.MainLoop()

Post a Comment for "Matplotlib Bar Chart In A Wx Frame Instead Of A New Window"