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
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)