残差图未与主图对齐

时间:2015-02-06 17:17:03

标签: python graph matplotlib

导致不与主图形对齐的剩余绘图有什么问题?我的代码如下。

enter image description here

import matplotlib.pyplot as plt
from scipy import stats
import numpy as np

x = np.array([0.030956,0.032956,0.034956,0.036956,0.038956,0.040956])
y = np.array([10.57821088,11.90701212,12.55570876,13.97542486,16.05403248,16.36634177])
yerr = [0.101614114,0.363255259,0.057234211,0.09289917,0.093288198,0.420165796]
xerr = [0.00021]*len(x)
fig1 = plt.figure(1)
frame1=fig1.add_axes((.1,.3,.8,.6))
m, b = np.polyfit(x, y, 1)
print 'gradient',m,'intercept',b
plt.plot(x, m*x + b, '-', color='grey', alpha=0.5) 
plt.plot(x,y,'.',color='black',markersize=6)
plt.errorbar(x,y,xerr=0,yerr=yerr,linestyle="None",color='black')
plt.ylabel('$1/\sqrt{F}$ $(N)$',fontsize=20)
plt.autoscale(enable=True, axis=u'both', tight=True)
plt.grid(False)
frame2=fig1.add_axes((.1,.1,.8,.2))
s = m*x+b #(np.sqrt(4*np.pi*8.85E-12)/2.23E-8)*x
difference = y-s
plt.plot(x, difference, 'ro')
frame2.set_ylabel('$Residual$',fontsize=20)
plt.xlabel('$2s+d_0$ $(m)$',fontsize=20)

2 个答案:

答案 0 :(得分:1)

您可以指定轴限制。问题是自动缩放会以不同方式移动您的两个图。如果插入2行代码,每行代码指定轴限制,它将修复它。

plt.axis([.030,.0415, 10, 17])  #line 17
plt.axis([.030,.0415, -.6, .8]) #line 26

enter image description here

我相信这是你正在寻找的。

答案 1 :(得分:0)

尝试使用GridSpec。

from matplotlib import gridspec
fig = plt.figure()
gs = gridspec.GridSpec(2, 1, height_ratios=[3, 1])
ax0 = plt.subplot(gs[0])
ax1 = plt.subplot(gs[1])
ax0.plot(x, m*x + b, '-', color='grey', alpha=0.5) 
ax0.plot(x,y,'.',color='black',markersize=6)
ax1.plot(x, difference, 'ro')

使用set_ylabel代替ylabel(例如你用于plt)。