如何从子图中的一个轴上删除标签?

发布于 2025-01-19 16:02:40 字数 802 浏览 4 评论 0原文

我在MacOS上使用Python 3.9。不久,我必须用4个子图制作一个地块,然后它们共享轴。代码看起来像这样:


#take some data
gs = gridspec.GridSpec(2, 2, height_ratios = [3, 1])
ax0 = plt.subplot(gs[0])

#plot data, make legend, etc.

ax2 = plt.subplot(gs[2], sharex = ax0)

#plot data, make legend, etc.

#take more data

ax1 = plt.subplot(gs[1], sharey = ax0)

#plot data, make legend, etc.

ax3 = plt.subplot(gs[3], sharex = ax1, sharey = ax2)

#plot data, make legend, etc.

plt.show()

如您所见,有些图彼此共享一个轴。问题在于,在X轴上,一切都很好,而它不在Y轴上(请参阅图片)。指出要点:如何删除右图的垂直轴上的数字,而不是在左侧?我看到了许多帖子,其中解决了问题,

ax.set_yticklabels([])

但这也消除了左图中的数字。

I am using Python 3.9 on MacOS. Shortly, I have to make a plot with 4 subplots, and they share axis. The code looks like this:


#take some data
gs = gridspec.GridSpec(2, 2, height_ratios = [3, 1])
ax0 = plt.subplot(gs[0])

#plot data, make legend, etc.

ax2 = plt.subplot(gs[2], sharex = ax0)

#plot data, make legend, etc.

#take more data

ax1 = plt.subplot(gs[1], sharey = ax0)

#plot data, make legend, etc.

ax3 = plt.subplot(gs[3], sharex = ax1, sharey = ax2)

#plot data, make legend, etc.

plt.show()

As you can see, some plots share an axis with each other. The problem is that on the x-axis everything is fine, while it is not on the y-axis (see picture). Getting to the point: how can I remove the numbers on the vertical axis of the right plot but not on the left? I've seen many posts in which the problem was solved with things like

ax.set_yticklabels([])

but that removes the numbers from the left plot as well.

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

终遇你 2025-01-26 16:02:40

尝试以下操作:

ax1.tick_params('y', labelleft=False)

Try this:

ax1.tick_params('y', labelleft=False)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文