生成子图的共享轴 matplotlib

Sharing axes for generated subplots matplotlib

好吧,我彻底搞糊涂了...
我有生成包含 8 个子图的图形的代码

import numpy as np
import matplotlib.pyplot as plt

names = ["one", "two", "three", "four", "five", "six", "seven", "eight"]
x = np.arange(1,11)
y = x**2.
z = x**1.5

fig = plt.figure()

fig.text(0.5, 0.02, "X axis", ha='center', fontsize=12)
fig.text(0.03, 0.5, "Y axis", va='center', rotation='vertical', fontsize=12)
palette = plt.get_cmap('tab10')

for name in names:
    i = names.index(name)+1 # What sub-plot are we on?
    graph = fig.add_subplot(4, 2, i)
# Add a new subplot to go in the grid (4x2)
    graph.set_title(f"plot {name}", fontsize=11)
    plot = graph.plot(x, y, color=palette(1))
    plot = graph.plot(x, z, color=palette(0))


plt.subplots_adjust(
top=0.93,
bottom=0.116,
left=0.124,
right=0.977,
hspace=0.972,
wspace=0.165)


plt.show()

它工作正常,除了我无法弄清楚如何设置子图以便它们共享 x 和 y 轴(我只想要 x 轴和左手边的底部边缘上的轴)。我能够找到的所有示例似乎都依赖于每个具有不同名称的子图,您可以使用这些名称来设置 sharexsharey。因为我是边生成边生成的,所以代码没有指向的地方。

根据评论,这是一个有效的答案

fig, axs = plt.subplots(4, 2, sharex='all', sharey='all')
graphs = axs.flatten()

for ax in graphs:
    ax.plot(x,y,color=palette(0))
    ax.plot(x,z,color= palette(1))

我已经从上面的 fig.addplot 版本更改为预先设置所有 sub-plots (plt.subplots(y,x)) 的版本,然后只使用循环来填充它们。

似乎仍然无法删除隐藏轴标签上的刻度线...