【发布时间】:2018-03-15 08:23:55
【问题描述】:
我一直在尝试将 matplotlib.widget.Button 添加到我的 matplotlib 绘图画布中,该画布已集成在 wxPython 中,但没有成功。
这是生成画布的代码:
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()
如何在 wxPython 中向这个 matplotlib 图的轴添加一个按钮?我试图遵循这个例子:https://matplotlib.org/examples/widgets/buttons.html,但没有成功,因为你没有在 wxPython 的 matplotlib 中使用plt.axes
我尝试了以下方法,实际上确实向画布添加了一个按钮,但它不是交互式的。
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')
这就是 GUI 画布现在的样子: Button that's not interactive
【问题讨论】:
-
如果你想坚持使用 matplotlib,我建议我们添加工具栏以允许基本导航。扩展工具栏see sample for MPL/TkInter 和sample for mpl/wxPython toolbar in general 不是火箭科学。而且至少在 wxPython 中,如果你在画布上,它实际上不可能将一个控件堆叠在另一个之上(你最终会得到一个按钮的“模拟”)。
-
我知道如何将工具添加到工具栏,但我希望在绘图本身中有两个“下一个”和“上一个”按钮。将这些按钮添加到工具栏不是我的偏好。
标签: python matplotlib wxpython matplotlib-widget