共享 x 和 y 轴时,如何为每个单独的绘图子图添加 x 和 y 标签?

时间:2021-01-11 19:29:22

标签: python matplotlib

我正在 jupyter notebook 中创建一个 3x3 的子图网格,其中共享 x 和 y 轴。

fig, ((ax1,ax2,ax3), (ax4,ax5,ax6), (ax7,ax8,ax9)) = plt.subplots(3,3, sharex=True, sharey=True)

x = np.linspace(0, 2*np.pi, 400)
y = np.sin(x**2)
ax5.plot(x,y,'-')enter code here

现在,当我尝试通过以下代码使每个子图的 x 和 y 标签可见时,没有任何反应。

for ax in plt.gcf().get_axes():
    for label in ax.get_xticklabels() + ax.get_yticklabels():
        label.set_visible(True)

当 x 和 y 轴共享时,如何获得每个子图的 x 和 y 标签?

1 个答案:

答案 0 :(得分:0)

根据the documentation

<块引用>

当子图具有沿列共享的 x 轴时,只有底部的 x 刻度标签 创建子图。类似地,当子图沿一行共享一个 y 轴时, 仅创建第一列子图的 y 刻度标签。 要稍后打开其他子图的刻度标签,请使用 tick_params

fig, axs = plt.subplots(3,3, sharex=True, sharey=True, constrained_layout=True)
for ax in axs.flat:
    ax.tick_params(labelbottom=True, labelleft=True)

enter image description here

相关问题