我有一个 pyqt5 小部件
class MPLibWidget(QtWidgets.QWidget):
"""
base MatPlotLib widget
"""
def __init__(self, parent=None):
super(MPLibWidget, self).__init__(parent)
self.figure = Figure()
self.canvas = FigureCanvasQTAgg(self.figure)
self.canvas.setParent(self)
self.mpl_toolbar = NavigationToolbar2QT(self.canvas, self)
self.canvas.mpl_connect('key_press_event', self.on_key_press)
self.axes = self.figure.add_subplot(111)
self.axes.hold(False)
当我尝试使用 result.plot_fit(ax=ax) 使用拟合时,它只绘制绘图拟合的数据列。不是 init_fit 或 best_fit。
def fit(self, data, widget):
self.layers = ['Air', 'BK7', 'SLG']
wv = np.arange(400, 900)
mod = lmfit.Model(self.get_R, ['wavelengths'], ['thickness', 'void_percent'])
mod.set_param_hint('thickness', value=130, min=50, max=250)
mod.set_param_hint('void_percent', value=.15, min=.05, max=.5)
R = data.norm(wv)
result = mod.fit(R, wavelengths=wv)
RMSE = (sp.sum(result.residual**2)/(result.residual.size-2))**0.5
bf_values = result.best_values
bf_str = 'thk: ' + str(round(bf_values['thickness'])) + ", Void %: " + str(round(bf_values['void_percent']*100, 2))
txt_spot = wv.min()-100 + (wv.max()-wv.min()) / 2
ax = widget.figure.axes[0]
result.plot_fit(ax=ax, datafmt='b+', initfmt='r--', fitfmt='g-')
ax.text(txt_spot, .9, "RMSE: "+str(round(RMSE, 3)))
ax.text(txt_spot, .85, bf_str)
widget.canvas.draw()
如果我不使用 (ax=ax) 而只使用 plot_fit(),那么 plt.show() 它会很好地绘制所有三个轴,但会打开一个新窗口。我希望情节显示在程序内的小部件中。