0

I have been trying to add a matplotlib.widget.Button to my matplotlib plotting canvas, which is integrated in wxPython, but without succes.

This is the code that generates the canvas:

    from matplotlib.figure import Figure
    from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
    import matplotlib.pyplot as plt

    import matplotlib
    matplotlib.use('WXAgg')

    from matplotlib.widgets import Button

    import wx


    self.figure = Figure()
    self.axes = self.figure.add_subplot(111)
    self.canvas = FigureCanvas(self, -1, self.figure)
    self.sizer = wx.BoxSizer(wx.VERTICAL)
    self.sizer.Add(self.canvas, proportion=1, flag=wx.ALL | wx.GROW)
    self.SetSizer(self.sizer)
    self.Fit()
    self.canvas.draw()


    plot_object = self.axes.pcolormesh(combo_value.T, cmap='rainbow', 
                                           norm=colors.LogNorm(vmin=vmin_value, vmax=vmax_value))

    self.canvas.draw()

How can I add a button to the axes of this matplotlib plot in wxPython? I have tried to follow this example: https://matplotlib.org/examples/widgets/buttons.html, but without succes since you do not use plt.axes in matplotlib in wxPython

I have tried the following which in fact does add a button to the canvas but it is not interactive.

    axprev = self.figure.add_axes([0.7, 0.01, 0.1, 0.075])
    bprev = Button(axprev, 'Previous')
    bprev.on_clicked(self.test())

def test(self):
    print('Called')

This is what the GUI canvas now looks like: Button that's not interactive

  • If you want stick with matplotlib, my recommendation would we to add the toolbar to allow basic navigation. It is no rocket science to extend the toolbar, [see sample for MPL/TkInter](https://stackoverflow.com/questions/23172916/matplotlib-tkinter-customizing-toolbar-tooltips) and [sample for mpl/wxPython toolbar in general](https://matplotlib.org/examples/user_interfaces/embedding_in_wx2.html). And besides at least in wxPython its not really possible to stack a control on top of another (you would end up with an "emulation" of a button) if you go on top of the canvas. – nepix32 Mar 15 '18 at 08:31
  • I know how to add tools to the toolbar but I would prefer to have two 'next' and 'previous' buttons in the plot itself. Adding these buttons to the toolbar does not have my preference. – Zwarte Viltstift Mar 15 '18 at 08:33

1 Answers1

0

This seems to be a problem of understanding the object oriented way of using matplotlib. Most pyplot commands directly have an OO equivalent as a method of the respective figure or axes in use.

Here is a translation of the example to object oriented style. (pyplpot is just used to create a figure and show the window, which you know how to do in wx as seen above.)

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Button

freqs = np.arange(2, 20, 3)

fig = plt.figure()   # use matplotlib.figure.Figure() for embedding
ax = fig.add_subplot(111)
fig.subplots_adjust(bottom=0.2)
t = np.arange(0.0, 1.0, 0.001)
s = np.sin(2*np.pi*freqs[0]*t)
l, = ax.plot(t, s, lw=2)


class Index(object):
    ind = 0

    def next(self, event):
        self.ind += 1
        i = self.ind % len(freqs)
        ydata = np.sin(2*np.pi*freqs[i]*t)
        l.set_ydata(ydata)
        fig.canvas.draw_idle()

    def prev(self, event):
        self.ind -= 1
        i = self.ind % len(freqs)
        ydata = np.sin(2*np.pi*freqs[i]*t)
        l.set_ydata(ydata)
        fig.canvas.draw_idle()

callback = Index()
axprev = fig.add_axes([0.7, 0.05, 0.1, 0.075])
axnext = fig.add_axes([0.81, 0.05, 0.1, 0.075])
bnext = Button(axnext, 'Next')
bnext.on_clicked(callback.next)
bprev = Button(axprev, 'Previous')
bprev.on_clicked(callback.prev)

plt.show()  # replace with your GUIs start of the mainloop

When using this inside a function or method don't forget to make the Buttons class variables,

    self.bnext = Button(axnext, 'Next')
    self.bnext.on_clicked(callback.next)
    self.bprev = Button(axprev, 'Previous')
    self.bprev.on_clicked(callback.prev)
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712