Search code examples
pythonmatplotlibpython-3.4pyqt5lmfit

lmfit, result.plot_fit(ax) not plotting all 3 plots in pyqt


I have a pyqt5 widget

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)

when I try to use the fit using result.plot_fit(ax=ax), it only plots the data column of the plot fit. Not the init_fit or the 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()

if I don't use (ax=ax) and just plot_fit(), then plt.show() it plots all three axes fine but it opens a new window. I want the plot to show up in my widget inside my program.

Inside widget with ax=ax Inside widget with plot_fit(ax=ax) Outside widget with just fit Outside widget with plot_fit(); plt.show()


Solution

  • The problem in the self.axes.hold(False) line which clears the figure before each call to plot and the like. plot_fit the last line plotted is 'data', hence all you get is the data line.

    Do not use hold(False) in general, we are thinking of removing it from upstream.