Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 每行共享y轴_Python_Python 3.x_Matplotlib_Subplot - Fatal编程技术网

Python 每行共享y轴

Python 每行共享y轴,python,python-3.x,matplotlib,subplot,Python,Python 3.x,Matplotlib,Subplot,我有一个关于使用matplotlib的子图每行共享y轴的问题。 我以下面的脚本为例。当我在子批函数中使用sharey=True作为参数,然后使用ax[x,x].set_ylimn,n时,它将使用我使用的最后一个参数 import matplotlib.pyplot as plt fig, ax = plt.subplots(3, 2, figsize=(8, 10), sharey=True, gridspec_kw={'height_ratios': [1, 2, 2]}) ax[0, 0]

我有一个关于使用matplotlib的子图每行共享y轴的问题。 我以下面的脚本为例。当我在子批函数中使用sharey=True作为参数,然后使用ax[x,x].set_ylimn,n时,它将使用我使用的最后一个参数

import matplotlib.pyplot as plt

fig, ax = plt.subplots(3, 2, figsize=(8, 10), sharey=True, gridspec_kw={'height_ratios': [1, 2, 2]})
ax[0, 0].set_ylim(-1.5, 1.5)
ax[1, 0].set_ylim(-40, 40)
ax[1, 1].set_ylim(-40, 40)
ax[2, 0].set_ylim(-40, 40)
ax[2, 1].set_ylim(-40, 40)

是否有一种方法可以共享每行的y轴

通过指定sharey='row',您可以做您想做的事情,如下所示。下面是一段代码,显示如何执行此操作:

from matplotlib import pyplot as plt
import numpy as np

x1 = np.linspace(0,2*np.pi,100)
x2 = np.linspace(-1,1,100)


f, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, sharey='row')
ax1.plot(x1, np.sin(x1))
ax1.set_title('sin(x)')

ax2.plot(x1, 2*np.cos(x1)+1)
ax2.set_title('2*cos(x)+1')

ax3.plot(x2, np.sqrt(np.abs(x2)))
ax3.set_title('sqrt(abs(x))')

ax4.plot(x2, x2**3)
ax4.set_title('x**3')

f.tight_layout()

plt.show()
这将产生以下结果:


你可以找到一个如何做到这一点的例子。简短回答:f,ax1,ax2,ax3,ax4=plt.subplots2,2,sharex='col',sharey='row'。谢谢你错过了这个例子