This is my code to plot a graph with residuals; however, I've lost my x-axis tick numbers and I don't know how to get them back... Any help would be much appreciated. And sorry if the formatting of this thread is wrong, it's my first.
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()
Instead of drawing frames, consider using subplots instead. As I see from the code you're attempting to have subplots with different height ratios. There are better ways to do this, for example with 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()
I have also added some consistency in the way you use pyplot
and plt
and removed some redundancies. Please go through the code side by side to see edits.
To answer your question, your x axis tick numbers are hiding behind the lower frame. As you have set height ratios to 0.8
for upper frame and 0.2
for lower frame, there is no space left for xticks.
Here is atleast something to go on.
The bottom "axes" (the "subplot" in the figure) is covering up the x-labels of the axes above.
You can see them again by making the bottom axes smaller
Use the line frame2=fig1.add_axes((.1,.1,.8,.1))
. (notice that the .2
is now .1
.
Your code doesn't run self-contained, but this is probably roughly what you're after:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.