有什么方法可以在matplotlib的部分子图中共享Y轴吗?

Phi*_*ats 2 python matplotlib

我有N个子图,除了其中一个,我想共享Y轴。可能吗?

MSe*_*ert 5

是的,您可以指定哪些支持与哪些轴共享哪个轴(这不是错字,我的意思是这句话)。有一个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()
Run Code Online (Sandbox Code Playgroud)

这会创建这样的曲线图(1 ST和2 共享y轴,但3 没有):

三个图,左图和中间图共有一个y轴。 右图具有独立的y比例。

您可以在matplotlib示例“ Shared axis Demo”中找到另一个示例。