Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show residual in the bottom of a matplotlib plot

I want to reproduce this plot. The errors are shown in the bottom of the plot. Can you please share how its done? enter image description here

There is an example that I found here on stackoverflow, but it is in R. How to create a graph showing the predictive model, data and residuals in R

like image 860
user1318806 Avatar asked Jun 09 '14 08:06

user1318806


1 Answers

You can create such plot in Matplotlib only by using add_axes. Here is an example.

from scipy.optimize import curve_fit
#Data
x = arange(1,10,0.2)
ynoise = x*numpy.random.rand(len(x)) 
#Noise; noise is scaled by x, in order to it be noticable on a x-squared function
ydata = x**2 + ynoise #Noisy data

#Model
Fofx = lambda x,a,b,c: a*x**2+b*x+c
#Best fit parameters
p, cov = curve_fit(Fofx,x,ydata)

#PLOT
fig1 = figure(1)
#Plot Data-model
frame1=fig1.add_axes((.1,.3,.8,.6))
#xstart, ystart, xend, yend [units are fraction of the image frame, from bottom left corner]
plot(x,ydata,'.b') #Noisy data
plot(x,Fofx(x,*p),'-r') #Best fit model
frame1.set_xticklabels([]) #Remove x-tic labels for the first frame
grid()

#Residual plot
difference = Fofx(x,*p) - ydata
frame2=fig1.add_axes((.1,.1,.8,.2))        
plot(x,difference,'or')
grid()

Plot residuals in the bottom by adding another frame using <code>add_axes</code>

like image 170
jaydeepsb Avatar answered Sep 28 '22 09:09

jaydeepsb