如何显示matplotlib p底部的残差

2024-06-16 10:11:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我想复制这个情节。错误显示在图的底部。你能告诉我是怎么做的吗? enter image description here

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


Tags: andthetodatamodel错误createstackoverflow
2条回答

只能使用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>

我想你在找这样的错误栏pylab_examples example code: errorbar_demo.py

您可以添加一个附加的子块,并用误差线绘制点。

编辑:绘图之间没有边框:

from pylab import *
subplots_adjust(hspace=0.,wspace=0.)
subplot(211)
imshow(rand(100,100), cmap=cm.BuPu_r)
subplot(212)
imshow(rand(100,100), cmap=cm.BuPu_r)
show()

相关问题 更多 >