我正在复制此post
上的代码这是我的代码
import numpy as np
f, axs = plt.subplots(1,2,figsize=(8,3))
x = np.array([])
y = np.array([])
for r in range(2):
x = np.append(x, np.array([-r,2*r,-r]))
y = np.append(y, np.array([2*r,-r,-r]))
axs[0].plot(x,y)
axs[0].scatter(x,y)
plt.xlim(-1,2)
plt.ylim(-1,2)
print(list(zip(x,y)))
x = np.array([])
y = np.array([])
for r in range(2):
x = np.append(x, np.array([-r,r,-r]))
y = np.append(y, np.array([r,-r,-r]))
axs[1].plot(x,y)
axs[1].scatter(x,y)
plt.xlim(-1,2)
plt.ylim(-1,2)
print(list(zip(x,y)))
两个轴上的刻度线不一致,如何解决?
答案 0 :(得分:2)
@ImportanceOfBeingErnest提供了一个很好的解决方案
使用axs[0].set_xlim
代替plt.xlim
import numpy as np
# f, axs = plt.subplots(1,2,figsize=(8,3),sharex=True, sharey=True)
f, axs = plt.subplots(1,2,figsize=(8,3))
x = np.array([])
y = np.array([])
for r in range(2):
x = np.append(x, np.array([-r,2*r,-r]))
y = np.append(y, np.array([2*r,-r,-r]))
axs[0].plot(x,y)
axs[0].scatter(x,y)
axs[0].set_xlim(-1,2)
axs[0].set_ylim(-1,2)
print(list(zip(x,y)))
x = np.array([])
y = np.array([])
for r in range(2):
x = np.append(x, np.array([-r,r,-r]))
y = np.append(y, np.array([r,-r,-r]))
axs[1].plot(x,y)
axs[1].scatter(x,y)
axs[1].set_xlim(-1,2)
axs[1].set_ylim(-1,2)
print(list(zip(x,y)))