简体   繁体   中英

How to add x and y labels for each individual plot subplots when x and y axis is shared?

I am creating a 3x3 grid of subplots with shared x and y axis in jupyter notebook.

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

Now when I try to make x and y labels for each individual subplot visible through following code, nothing happens.

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

How do I get x and y labels for each subplot when x and y axis are shared?

As per the documentation :

When subplots have a shared x-axis along a column, only the x tick labels of the bottom subplot are created. Similarly, when subplots have a shared y-axis along a row, only the y tick labels of the first column subplot are created. To later turn other subplots' ticklabels on, use 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)

在此处输入图像描述

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM