python - 如何在 matplotlib 图的底部显示残差

标签 python matplotlib plot

我想重现这个情节。错误显示在图的底部。你能分享一下它是怎么做到的吗? enter image description here

我在 stackoverflow 上找到了一个示例,但它是在 R 中。 How to create a graph showing the predictive model, data and residuals in R

最佳答案

您只能使用 add_axes 在 Matplotlib 中创建此类绘图.这是一个例子。

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>

关于python - 如何在 matplotlib 图的底部显示残差,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24116318/

相关文章:

python - 根据键 ID 选择 appengine 数据库中的行

python - 在 Numpy、Python 中“拉伸(stretch)”直方图(级别)

python - Tkinter 中 matplotlib 图形周围的黑框

jquery - float 相同范围的刻度

python - 如何在Python中找到给定字符串中的最大数字?

python 单元测试来测试循环而不是中断

python - 使用 Ipython 调用 pylab 的 GUI 请求无效

python - 在散点图上叠加线函数 - seaborn

R:在线图中,如何控制每个点(而不是每个系列)使用的角色?

matlab - 两个时间序列图和它们之间的阴影......MATLAB