我有两个绘图脚本,它们使用subplots
生成图形axis[i].grid(axis = 'y')
仅显示y轴的网格。其他的很好,但是我不知道为什么这个脚本没有显示网格线。有人能帮我吗
import matplotlib.pyplot as plt
import numpy as np
nel = 8
npts = 10
h = 1./nel
x = np.zeros((npts+1, nel))
x_c = np.zeros(nel)
for i in range(0, nel):
x[:,i] = np.linspace(i*h, (i+1)*h, npts+1)
x_c[i] = (i+0.5) * h
print(x)
print(x_c)
# 3rd order DGD basis
fig, axs = plt.subplots(2, 1, sharex = True, figsize = (6,10), facecolor = 'w')
plt.tight_layout()
fig.subplots_adjust(hspace = 0)
y = np.zeros((npts+1, nel)) # for 0th element
y[:,0] = (x[:,0] - x_c[1]) * (x[:,0] - x_c[2]) * (x[:,0] - x_c[3])/(-6*h**3)
y[:,1] = (x[:,1] - x_c[1]) * (x[:,1] - x_c[2]) * (x[:,1] - x_c[3])/(-6*h**3)
for j in range(0,nel):
axs[0].plot(x[:,j], y[:,j], 'r-', linewidth=2)
axs[0].set_xticks(np.arange(0, 1.01, h))
axs[0].set_yticks(np.arange(0, 1.2, 1.0))
axs[0].set_ylim(-0.5, 1.5)
axs[0].grid(axis = 'y')
y = np.zeros((npts+1, nel)) # for 1st element
y[:,0] = (x[:,0] - x_c[0]) * (x[:,0] - x_c[2]) * (x[:,0] - x_c[3])/(2*h**3)
y[:,1] = (x[:,1] - x_c[0]) * (x[:,1] - x_c[2]) * (x[:,1] - x_c[3])/(2*h**3)
y[:,2] = (x[:,2] - x_c[2]) * (x[:,2] - x_c[3]) * (x[:,2] - x_c[4])/(-6*h**3)
for j in range(0,nel):
axs[1].plot(x[:,j], y[:,j],'r-', linewidth=2)
axs[1].set_xticks(np.arange(0, 1.01, h))
axs[1].set_yticks(np.arange(0, 1.2, 1.0))
axs[1].set_ylim(-0.5, 1.5)
axs[1].grid(axis = 'y')
plt.show()
在for循环之外设置
axs[0].grid(axis = 'y')
和其他轴设置似乎可以使其工作,只是在Jupyter笔记本中测试了它,例如:相关问题 更多 >
编程相关推荐