在matplotlib中,有没有一种方法可以在部分子图之间共享Y轴?

6

我有 N 个 subplot,我想让其中除了一个 subplot,其余的 subplot 共享 Y 轴。这是否可能?

2个回答

9

是的,你可以指定哪些子图与哪些轴共享,有sharexsharey参数可用于add_subplot

例如:

import numpy as np
import matplotlib.pyplot as plt

x = np.array([1,2,3,4,5])
y1 = np.arange(5)
y2 = y1 * 2
y3 = y1 * 5

fig = plt.figure()
ax1 = fig.add_subplot(131)                # independant y axis (for now)
ax1.plot(x, y1)
ax2 = fig.add_subplot(132, sharey=ax1)    # share y axis with first plot
ax2.plot(x, y2)
ax3 = fig.add_subplot(133)                # independant y axis
ax3.plot(x, y3)
plt.show()

这将创建一个像这样的图表(第一和第二共用y轴,但第三个不共用): Three plots, the left and middle one share a common y axis. The right plot has an independent y scale. 您可以在matplotlib示例中找到另一个示例:“共享轴演示”

3
如果您正在使用这种格式。
fig, axs = plt.subplots(4,1,sharex=True,figsize=(8,8))

你可以尝试。
axs[2].sharey(axs[1])

axs[3].sharey(axs[1])

每个子图之后。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接