2017-03-04 32 views
0

我的python图有奇怪的双层文本和作为初学者我不知道如何清理它。这将如何完成?如何修复python图形上的双层文本

由于

import matplotlib.pyplot as plt 
import numpy as np 

fig1 = plt.figure(1) 
plt.xticks(np.arange(0, 701, 100)) 
plt.yticks(np.arange(0.0, 3.7, 0.5)) 

frame1=fig1.add_axes((0.1,0.3,.8,.6)) 
m, b = np.polyfit(x, y, 1) 
plt.plot(x, m*x + b, '-', color='grey', alpha=0.5) 
plt.plot(x,y,'.',color='navy',markersize=6) 
plt.errorbar(x,y,xerr=0,yerr=yerr,linestyle="None",color='navy') 
plt.ylabel('$Natural\ Log\ of\ Rate$',fontsize=17) 
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=17) 
plt.xlabel('$Time$ $(s)$',fontsize=17) 
plt.savefig('mygraph') 

plt.show() 
+0

虽然很明显,该地块并不好看,目前还不清楚你想要什么,而不是。哪个轴应该在哪里?两张图都应该在同一个坐标轴上吗?考虑阅读[问]并提供[mcve](这里是什么'x','y'和'yerr'?)。 – ImportanceOfBeingErnest

回答

1

您可能需要让matplotlib自动使用fig.add_subplot(211),其中211意味着在的2行1列,第一(顶部)副区栅格应使用定位副区。

然后,使用API​​方法绘制到轴,ax.plot()而不是plt.plot(),并使用API​​方法设置所有其他标签和刻度都很方便。这使得更容易确定哪些元素属于哪个子图。

import matplotlib.pyplot as plt 
import numpy as np 

x = np.arange(0, 700, 100) 
y = np.arange(0.0, 3.5, 0.5) 

fig1 = plt.figure(1) 

ax1=fig1.add_subplot(211) 
m, b = np.polyfit(x, y, 1) 

ax1.set_xticks(np.arange(0, 701, 100)) 
ax1.set_yticks(np.arange(0.0, 3.7, 0.5)) 
ax1.plot(x, m*x + b, '-', color='grey', alpha=0.5) 
ax1.plot(x,y,'.',color='navy',markersize=6) 
ax1.set_ylabel('$Natural\ Log\ of\ Rate$',fontsize=17) 
plt.grid(False) 

ax2=fig1.add_subplot(212) 
s = m*x+b #(np.sqrt(4*np.pi*8.85E-12)/2.23E-8)*x 
difference = y-s 
ax2.plot(x, difference, 'ro') 
ax2.set_ylabel('$Residual$',fontsize=17) 
ax2.set_xlabel('$Time$ $(s)$',fontsize=17) 

plt.show() 

enter image description here