How to embed MatPlotLib in wxPython classic

266 views
Skip to first unread message

Umar Yusuf

unread,
Aug 22, 2016, 11:29:34 AM8/22/16
to wxpytho...@googlegroups.com
Hello,

Kindly guide me using a simple step-by-step approach on embeding a matplotlib figure in a wxPython classic GUI.

The matplotlib documentation on WXAgg is difficult for me to uderstand. And I coldn't find a simple tutorial online.

Hope to hear from you soon.
Thanks for your time in advance.

My system configurations: Windows 7, Python 2.7.12, and wxPython 3.0-msw classic



# ========= MatPlotLib Figure Appear in Blue part while controls at right and bottom ================#

import wx
import wx.xrc
class MyFrame1 ( wx.Frame ):
	
	def __init__( self, parent ):
		wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = wx.EmptyString, pos = wx.DefaultPosition, size = wx.Size( 700,500 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )
		
		self.SetSizeHintsSz( wx.DefaultSize, wx.DefaultSize )
		
		bSizer1 = wx.BoxSizer( wx.HORIZONTAL )
		
		bSizer5 = wx.BoxSizer( wx.VERTICAL )
		
		bSizer6 = wx.BoxSizer( wx.VERTICAL )
		
		self.m_staticText3 = wx.StaticText( self, wx.ID_ANY, u"MatPlotLib Graph Here\n\n?\n\n?", wx.DefaultPosition, wx.DefaultSize, wx.ALIGN_CENTRE )
		self.m_staticText3.Wrap( -1 )
		self.m_staticText3.SetFont( wx.Font( 20, 70, 90, 90, False, wx.EmptyString ) )
		self.m_staticText3.SetBackgroundColour( wx.SystemSettings.GetColour( wx.SYS_COLOUR_HIGHLIGHT ) )
		
		bSizer6.Add( self.m_staticText3, 1, wx.ALL|wx.EXPAND|wx.ALIGN_CENTER_HORIZONTAL, 5 )
		
		
		bSizer5.Add( bSizer6, 1, wx.EXPAND, 5 )
		
		bSizer7 = wx.BoxSizer( wx.VERTICAL )
		
		self.m_staticText1 = wx.StaticText( self, wx.ID_ANY, u"Controls Here", wx.DefaultPosition, wx.DefaultSize, 0 )
		self.m_staticText1.Wrap( -1 )
		self.m_staticText1.SetBackgroundColour( wx.SystemSettings.GetColour( wx.SYS_COLOUR_BTNSHADOW ) )
		
		bSizer7.Add( self.m_staticText1, 0, wx.ALL|wx.EXPAND, 5 )
		
		
		bSizer5.Add( bSizer7, 0, wx.EXPAND, 5 )
		
		
		bSizer1.Add( bSizer5, 1, wx.EXPAND, 5 )
		
		bSizer8 = wx.BoxSizer( wx.VERTICAL )
		
		self.m_staticText2 = wx.StaticText( self, wx.ID_ANY, u"Controls Here", wx.DefaultPosition, wx.DefaultSize, 0 )
		self.m_staticText2.Wrap( -1 )
		self.m_staticText2.SetBackgroundColour( wx.SystemSettings.GetColour( wx.SYS_COLOUR_BTNSHADOW ) )
		
		bSizer8.Add( self.m_staticText2, 1, wx.ALL|wx.ALIGN_CENTER_HORIZONTAL, 5 )
		
		
		bSizer1.Add( bSizer8, 0, wx.EXPAND, 5 )
		
		
		self.SetSizer( bSizer1 )
		self.Layout()
		
		self.Centre( wx.BOTH )
	
	def __del__( self ):
		pass
	

app = wx.App()
frame = MyFrame1(None).Show()
app.MainLoop()










--

**************************************

*Wouldn't you rather do Business with us?  *   
This message is for the designated recipient only and may contain
privileged, proprietary, or otherwise private information. If you have
received it in error, please notify the sender immediately and delete the
original. Any use of the email by you is prohibited. If you have received
this communication in error, please notify the author by replying to this
e-mail immediately.

**************************************

www.BintaSMS.com <http://www.bintasms.com/>
www.BintaComputers.net <http://www.bintacomputers.net/>



Vlastimil Brom

unread,
Aug 22, 2016, 2:56:54 PM8/22/16
to wxPython-users
> ...

Hi,
as there isn't any matplotlib-specific code in the sample, however,
I'd recommend looking at the code examples at
http://matplotlib.org/examples/index.html
especially in the section
user_interfaces Examples
http://matplotlib.org/examples/user_interfaces/index.html
there are several examples for "embedding_in_wx ...", which might be
directly relevant for your use case.
e.g.
http://matplotlib.org/examples/user_interfaces/embedding_in_wx4.html
shows the usage for the built-in toolbar for the figure, as well as
adding custom control.

hth,
vbr

Marco Prosperi

unread,
Aug 24, 2016, 3:17:39 AM8/24/16
to wxPython-users


you can learn something from this

http://agni.phys.iit.edu/~kmcivor/wxmpl/

for interactive usage (pyshell) I use this

>>> import matplotlib
>>> matplotlib.use('WXAgg')
>>> matplotlib.interactive(True)
>>> from pylab import *
>>> plot([1, 2, 3])

Dietmar Schwertberger

unread,
Aug 24, 2016, 5:38:56 AM8/24/16
to wxpytho...@googlegroups.com

With Python 3.5 and Phoenix I'm using code like this to embed a canvas into my GUI:

        import matplotlib
        from matplotlib.figure import Figure
        #from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
        from matplotlib.backends.backend_wx import FigureCanvasWx as FigureCanvas
        f = self.matplotlib_figure = Figure()
        self.matplotlib_axes = f.add_subplot(111) # 1x1 grid, first subplot
        import numpy
        t = numpy.arange(0.0,10,1.0)
        s = [0,1,0,1,0,2,1,2,1,0]
        ymax = 10
        self.matplotlib_axes.plot(t,s)
        c = FigureCanvas(parent_window, -1, f)
        c.SetSize( (200,200) )

The canvas c can then be added to a sizer just as any other object.
Unfortunately, matplotlib has no good way to export plots into Microsoft Office, so I'm always using my own wx based plot library...

Regards,

Dietmar
-- You received this message because you are subscribed to the Google Groups "wxPython-users" group. To unsubscribe from this group and stop receiving emails from it, send an email to wxpython-user...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.

Matt Newville

unread,
Aug 24, 2016, 10:07:09 AM8/24/16
to wxpytho...@googlegroups.com
Umar,

On Mon, Aug 22, 2016 at 10:29 AM, Umar Yusuf <bintaco...@gmail.com> wrote:
Hello,

Kindly guide me using a simple step-by-step approach on embeding a matplotlib figure in a wxPython classic GUI.

The matplotlib documentation on WXAgg is difficult for me to uderstand. And I coldn't find a simple tutorial online.

Hope to hear from you soon.
Thanks for your time in advance.

My system configurations: Windows 7, Python 2.7.12, and wxPython 3.0-msw classic


You may find wxmplot useful: https://github.com/newville/wxmplot/

For simple 2D plots this provides a PlotPanel class -- a subclass of wx.Panel with "plot()" and other methods for plotting data, using matplotlib's WXAgg canvas.  The wxmplot.PlotPanel provide built-in interactivity (rubber-band to zoom, etc), and the properties of the plot (colors, linewidths, markers, titles, etc) can be modified by the end-user from a configuration panel.    There is some documentation and examples provided in the source distribution.
 
If this doesn't do what you want, check the source code as an example for embedding a FigureCanvasWxAgg in a wx.Panel (https://github.com/newville/wxmplot/blob/master/lib/plotpanel.py#L533). 

Suggestions for improvements or additions are most welcome.

FWIW, wxmplot works with both wxPython Classic and Phoenix, and works for me with Python 2.7 and Python 3.5.

Dietmar Schwertberger wrote:

>  Unfortunately, matplotlib has no good way to export plots into Microsoft Office, so I'm
> always using my own wx based plot library...

This is not completely accurate, though it might be that the standard pylab.plot resulting Frame does not have a good way to export plots.

With wxmplot, Ctrl-C (or Apple-C on Mac OS X) from the PlotPanel copies the image of the panel to the system Clipboard which can then be pasted into many Windows/Mac apps (and certainly Office apps) with Ctrl-V/Apple-V.   In addition, saving the image (Ctrl-S/Apple-S) to a PNG file gives a publication quality image (I have published several of these plots directly from wxmplot/matplotlib) that can easily be included into many apps, including Office apps.

Hope that helps,
 
--Matt Newville

Dietmar Schwertberger

unread,
Aug 24, 2016, 11:12:31 AM8/24/16
to wxpytho...@googlegroups.com
On 24.08.2016 16:06, Matt Newville wrote:
Dietmar Schwertberger wrote:
>  Unfortunately, matplotlib has no good way to export plots into Microsoft Office, so I'm
> always using my own wx based plot library...

This is not completely accurate, though it might be that the standard pylab.plot resulting Frame does not have a good way to export plots.

With wxmplot, Ctrl-C (or Apple-C on Mac OS X) from the PlotPanel copies the image of the panel to the system Clipboard which can then be pasted into many Windows/Mac apps (and certainly Office apps) with Ctrl-V/Apple-V.   In addition, saving the image (Ctrl-S/Apple-S) to a PNG file gives a publication quality image (I have published several of these plots directly from wxmplot/matplotlib) that can easily be included into many apps, including Office apps.
Well, that's what I mean with "no good way", as PNG ist not an option for me when I want to create PDFs.
For PDFs I want to have vector graphics. Images are either ugly or create large files. Also EMF/WMF plots have the advantages that they can be post-processed more easily when you want to shift or add something.
There used to be a EMF backend, but this is no longer available.
Unfortunately, the wx matplotlib backend uses matplotlib to render an image and then paint it to the screen.

Any volunteers to implement a wx vector backend? I would have considered doing this, but currently I'm busy in re-working wxGlade...

Regards,

Dietmar

Matt Newville

unread,
Aug 24, 2016, 11:56:37 AM8/24/16
to wxpytho...@googlegroups.com
On Wed, Aug 24, 2016 at 10:12 AM, Dietmar Schwertberger <mail...@schwertberger.de> wrote:
On 24.08.2016 16:06, Matt Newville wrote:
Dietmar Schwertberger wrote:
>  Unfortunately, matplotlib has no good way to export plots into Microsoft Office, so I'm
> always using my own wx based plot library...

This is not completely accurate, though it might be that the standard pylab.plot resulting Frame does not have a good way to export plots.

With wxmplot, Ctrl-C (or Apple-C on Mac OS X) from the PlotPanel copies the image of the panel to the system Clipboard which can then be pasted into many Windows/Mac apps (and certainly Office apps) with Ctrl-V/Apple-V.   In addition, saving the image (Ctrl-S/Apple-S) to a PNG file gives a publication quality image (I have published several of these plots directly from wxmplot/matplotlib) that can easily be included into many apps, including Office apps.
Well, that's what I mean with "no good way", as PNG ist not an option for me when I want to create PDFs.
For PDFs I want to have vector graphics. Images are either ugly or create large files. Also EMF/WMF plots have the advantages that they can be post-processed more easily when you want to shift or add something.
There used to be a EMF backend, but this is no longer available.
Unfortunately, the wx matplotlib backend uses matplotlib to render an image and then paint it to the screen.


OK, I understand.

FWIW, I find the PNGs from matplotlib to be very high quality, and am very happy they can readily render TeX strings for mathematical symbols.  I do tend to make high density or large images, but I wouldn't call them large files (rarely > 0.5MB).   I have no trouble embedding these in MS Office docs, latex-generaed PDFs, or web pages.  But, like you say, they are definitely images and the plot components are not easily manipulated later.

Matplotlib can also save to SVG, which is a vector graphics format and so might be closer to what you want. I believe these are not immediately used by MS Office, but can be converted to EMF or WMF, though I do not know any details.

Any volunteers to implement a wx vector backend? I would have considered doing this, but currently I'm busy in re-working wxGlade...


Would SVG work? 

--Matt

Dietmar Schwertberger

unread,
Aug 24, 2016, 12:25:00 PM8/24/16
to wxpytho...@googlegroups.com
On 24.08.2016 17:55, Matt Newville wrote:
FWIW, I find the PNGs from matplotlib to be very high quality, and am very happy they can readily render TeX strings for mathematical symbols.  I do tend to make high density or large images, but I wouldn't call them large files (rarely > 0.5MB).   I have no trouble embedding these in MS Office docs, latex-generaed PDFs, or web pages.  But, like you say, they are definitely images and the plot components are not easily manipulated later.
When you have ten plots, then you are at several MBs already, while the rest of our datasheets is really compact (500k with the vector graphics and some images).
Honestly, I also just dislike images in PDFs as there are so many PDFs out there with diagrams converted to very ugly low-resolution images.

I also like and use the TeX notation, even though I just use it for sub- and superscripts.


Matplotlib can also save to SVG, which is a vector graphics format and so might be closer to what you want. I believe these are not immediately used by MS Office, but can be converted to EMF or WMF, though I do not know any details.

Would SVG work? 
Unfortunately, Microsoft still does not support SVG in Office. This will probably never change, even though Internet Explorer can view SVG.
Currently, I'm calling Inkscape from my scripts to convert the SVGs to EMF, but that's not too nice, especially when it comes to clipping...
Not sure about MS Visio. I think I tried it at one point, but sticked to Inkscape.

Regards,

Dietmar

Reply all
Reply to author
Forward
0 new messages