Hi, I need some help on how I could add the following pie chart to a wxPanel. I just can't seem to get it to work without the pylab interface.

import wx
from pylab import *

class MyFrame(wx.Frame):
    """ Pie Chart Frame """
    def __init__(self):
        wx.Frame.__init__(self,None,-1)

        self.panel=wx.Panel(self,-1)


        """ Start Pie Chart Code"""
        # make a square figure and axes
        figure(1, figsize=(6,6))
        ax = axes([0.1, 0.1, 0.8, 0.8])

        labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
        fracs = [15,30,45, 10]

        explode=(0, 0.05, 0, 0)
        pie(fracs, explode=explode, labels=labels, autopct='%1.1f%%', shadow=True)
        title('Raining Hogs and Dogs', bbox={'facecolor':'0.8', 'pad':5})
        show()
        """ End Pie Chart Code"""

        
class MyApp(wx.App):
    def OnInit(self):
        frame = MyFrame()
        frame.Show()
        return True

app = MyApp(redirect=False)
app.MainLoop()
app.Destroy()

Pie Chart Source...

Dani AI

Generated

The original approach used pylab/show() (opens a separate Matplotlib window) and the follow-up from (crediting @vagaseat) writes the chart to PNG and displays that with wx.StaticBitmap. That works, but it incurs disk I/O and gives no interactivity or smooth resizing. A cleaner, more modern pattern is to use Matplotlib's object-oriented API and embed a FigureCanvas directly in the wx.Panel — no temporary files, full interaction, and proper layout control.

Example (embed a Matplotlib Figure in a wx.Panel):

import wx
from matplotlib.figure import Figure
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas

class PiePanel(wx.Panel):
    def __init__(self, parent):
        super().__init__(parent)
        fig = Figure(figsize=(6, 6))
        ax = fig.add_axes([0.1, 0.1, 0.8, 0.8])
        labels = ['Frogs', 'Hogs', 'Dogs', 'Logs']
        fracs = [15, 30, 45, 10]
        explode = (0, 0.05, 0, 0)
        ax.pie(fracs, explode=explode, labels=labels, autopct='%1.1f%%', shadow=True)
        canvas = FigureCanvas(self, -1, fig)
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(canvas, 1, wx.EXPAND)
        self.SetSizer(sizer)
        canvas.draw()

Troubleshooting and tips:

  • Avoid pylab/plt.show() when embedding; it creates a separate window. Use the Figure/axes API instead.
  • If Matplotlib backend errors appear, ensure the WXAgg backend is selected before importing pyplot (or just use Figure + FigureCanvas as above).
  • Use sizers with proportion=1 and wx.EXPAND so the canvas resizes with the frame; call canvas.draw() after updating the plot.
  • If a static image is acceptable and disk writes must be avoided, render to an in-memory buffer (BytesIO) and load into a wx.Bitmap — but embedding the canvas is simpler for interactive UI.

Credit to @vagaseat for the PNG trick; embedding the canvas is recommended for cleaner integration and better UX.

Solved it thanks to a post by vagaseat here...
http://www.daniweb.com/forums/thread239191.html

import wx
import pylab

class MyFrame(wx.Frame):
    """ Pie Chart Frame """
    def __init__(self):
        wx.Frame.__init__(self,None,-1)

        self.panel=wx.Panel(self,-1)

        """ Start Pie Chart Code"""
        # make a square figure and axes
        pylab.figure(1, figsize=(6,6))
        ax = pylab.axes([0.1, 0.1, 0.8, 0.8])

        labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
        fracs = [15,30,45, 10]

        explode=(0, 0.05, 0, 0)
        pylab.pie(fracs, explode=explode, labels=labels, autopct='%1.1f%%', shadow=True)
        pylab.title('Raining Hogs and Dogs', bbox={'facecolor':'0.8', 'pad':5})

        #plt.show()
        pylab.savefig("test.png")

        # create an internal image
        image = wx.Bitmap("test.png")
        # show the image as static bitmap
        wx.StaticBitmap(self, wx.ID_ANY, image)

        perf_plot = 'test.png'

        self.Fit()
        """ End Pie Chart Code"""
   
class MyApp(wx.App):
    def OnInit(self):
        frame = MyFrame()
        frame.Show()
        return True

app = MyApp(redirect=False)
app.MainLoop()
app.Destroy()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.