这是我用残差绘制图表的代码;但是,我已经丢失了我的x轴刻度数字,而且我不知道如何让它们回来......任何帮助都会非常感激。对不起,如果这个帖子的格式错误,那就是我的第一个。
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 :(得分: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()
我还在使用pyplot
和plt
的方式中添加了一些一致性,并删除了一些冗余。请仔细阅读代码以查看修改。
要回答您的问题,您的x轴刻度数字会隐藏在较低的框架后面。由于您已为上部框架将高度比率设置为0.8
而下部框架的高度比率设置为0.2
,因此xticks没有剩余空间。
至少还有一些事情可以继续。
答案 1 :(得分:0)