Anthony Nork Anthony Nork - 11 months ago 37
Python Question

show fitparameters in plotlegend with pyplot

if I do a fit on some data I got and want to plot that I also want to Show the fitparameters which I get as well as reduced chi square and r square in the legend of my plot.
What would I have to do to get this done?

Sorry if that is a trivial question, I'm new to python so I might not know or see the obvious thing.

Answer Source

Here is how to perform a fit and plot the result in a legend in matplotlib.

import numpy as np
import matplotlib.pyplot as plt
import scipy.optimize

x = np.arange(10)
y = np.arange(10)/5.+np.random.rand(10)

func = lambda x, a, b: a*x+b
popt, pcov = scipy.optimize.curve_fit(func, x,y)

plt.plot(x,y, marker="o", linestyle="none", label="data")

plt.plot(x,func(x, popt[0], popt[1]), label="fit"+str(popt))


enter image description here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download