【问题标题】:How to show residual in the bottom of a matplotlib plot如何在 matplotlib 图的底部显示残差
【发布时间】:2014-06-09 08:23:31
【问题描述】:

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

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

【问题讨论】:

标签: python matplotlib plot


【解决方案1】:

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

【讨论】:

  • 不起作用,给出错误。图未定义
  • 如果 'figure not defined' 是错误,那么我猜你必须从 pylab 包中导入它,例如 from pylab import *
【解决方案2】:

我认为您正在寻找这样的错误栏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()

【讨论】:

  • 您的链接没有显示如何将这些图连接在一起。
  • 我认为错误栏是问题所在。我从来没有这样做过,但在快速谷歌搜索后,我会试试这个:matplotlib.org/api/…
  • 我用示例代码更新了我的答案,以消除子图之间的差距
猜你喜欢
  • 2018-10-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-04
  • 1970-01-01
  • 2020-10-22
  • 1970-01-01
  • 2021-12-28
相关资源
最近更新 更多