2017-12-02 155 views
0

這是我繪製殘差圖的代碼;然而,我已經失去了我的X軸刻度數字,我不知道如何讓他們回來......任何幫助將不勝感激。對不起,如果這個線程的格式不對,這是我的第一個。我已經在圖表的x-ticks上丟失了數字,我該如何回覆它們?

代碼:

pyplot.figure() 

fig1 = plt.figure(1) 

frame1=fig1.add_axes((.1,.3,.8,.6)) 

pyplot.errorbar(xval, yval, yerr=yerr, xerr=xerr, marker='o',markersize = 2, linestyle='None', color = 'black') 

# Axis labels 
pyplot.xlabel(' Height (m)') 

pyplot.ylabel('Mass per second (kg.s-1)') 

# Generate best fit line using model function and best fit parameters, and add to plot 
fit_line=model_funct(xval, [a_soln, b_soln]) 

# Theoretical line 
x = np.array(arange(0.07,0.15, 0.001)) 

y = (-2.61049E-05) + (0.005815772)*x 

plt.plot(x, y, linestyle = '--', color ='r',linewidth = 0.7, label = 'Theoretical') 

# Experimental line 
s = (-4.43329E-05) + (0.006008837)*x 

pyplot.plot(x, s,linewidth = 0.7, color = 'black', label = 'Experimental Chi Squared Fit') 

# Set suitable axis limits: you will probably need to change these... 
pyplot.xlim(0.08, 0.14) 

pyplot.ylim(0.0004, 0.0008) 

pyplot.legend(loc = 'upper left',prop={'size':10}) 

frame2=fig1.add_axes((.1,.1,.8,.2)) 

difference = y - s 

pyplot.plot(x, difference, color = 'black') 

frame2.set_ylabel('Residual') 

plt.xlabel('Height (m)') 

plt.yticks(numpy.arange(-0.000010, 0.000010, 0.00001)) 

plt.xticks(numpy.arange(0.08, 0.14, 0.01)) 

pyplot.ylim(-0.000010, 0.000010) 

pyplot.xlim(0.08,0.14) 

pyplot.grid() 


pyplot.show() 
+0

如果你提供的一個小例子,這將是有益的工作代碼(https://stackoverflow.com/help/mcve)您提供的代碼與格式混合,不包含'import'語句,並引用未定義的變量,例如'xval'。 – Gus

回答

0

相反牽伸裝置中,可以考慮使用次要情節代替。正如我從代碼中看到的那樣,您試圖使用不同高度比例的子圖。有更好的方法來做到這一點,例如與gridspec

import numpy as np 
import matplotlib.pyplot as plt 
import matplotlib.gridspec as gridspec 


fig = plt.figure() 
gs = gridspec.GridSpec(2, 1, height_ratios=[4, 1]) 
ax1 = plt.subplot(gs[0, 0]) 
plt.errorbar(xval, yval, yerr=yerr, xerr=xerr, marker='o', markersize = 2, linestyle='None', color='black') 
#Axis labels 
ax1.set_xlabel(' Height (m)') 
ax1.set_ylabel(r'Mass per second (kg$\cdot$s$^{-1}$)') 
#Generate best fit line using model function and best fit parameters, and add to plot 
fit_line=model_funct(xval, [a_soln, b_soln]) 
#Theoretical line 
x = np.arange(0.07, 0.15, 0.001) 
y = (-2.61049E-05) + (0.005815772)*x 
ax1.plot(x, y, linestyle = '--', color ='r',linewidth = 0.7, label = 'Theoretical') 

#Experimental line 
s = (-4.43329E-05) + (0.006008837)*x 
ax1.plot(x, s,linewidth = 0.7, color = 'black', label = 'Experimental Chi Squared Fit') 

#Set suitable axis limits: you will probably need to change these... 
ax1.set_xlim(0.08, 0.14) #redundant by adding "sharex=ax1" 4 lines blelow 
ax1.set_ylim(0.0004, 0.0008) 
ax1.legend(loc = 'upper left',prop={'size':10}) 

ax2 = plt.subplot(gs[1, 0], sharex=ax1) 

difference = y - s 

plt.plot(x, difference, color = 'black') 

ax2.set_ylabel('Residual') 

ax2.set_xlabel('Height (m)') 

ax2.set_yticks(np.arange(-0.000010, 0.000010, 0.00001)) 
ax2.set_xticks(np.arange(0.08, 0.14, 0.01)) 

ax2.set_ylim(-0.000010, 0.000010) 
ax2.set_xlim(0.08,0.14) 

ax2.grid() 
plt.tight_layout() 
plt.show() 

我還添加了一些一致性您使用pyplotplt的方式,去掉了一些冗餘。請通過代碼並排查看編輯。

要回答您的問題,您的x軸刻度編號隱藏在較低的框架後面。由於您已將較高幀設置爲0.8,較低幀設置爲0.2,因此xticks沒有空間。

這是至少要繼續下去的東西。

enter image description here

0

底部「軸」(該圖中的「副區」)被掩蓋上述的軸的x標籤。

您可以通過下方再次見到他們軸小

使用行frame2=fig1.add_axes((.1,.1,.8,.1))。 (注意到.2現在.1

你的代碼不能運行自包含的,但是這可能是大概你追求的: enter image description here

相關問題