Python 具有共享轴的matplotlib子批次

Python 具有共享轴的matplotlib子批次,python,matplotlib,Python,Matplotlib,我很难理解matplotlib子地块如何允许它们之间共享轴。我看到了一些示例,但我无法修改其中一个以适合我的用例。。;在这里,我用制服替换了我的数据,这样绘图就不会有趣了,但不管怎样 import numpy as np import matplotlib.pyplot as plt import matplotlib as mpl from matplotlib import cm d = 4 n1 = 100000 n2 = 100 background_data = np.random

我很难理解matplotlib子地块如何允许它们之间共享轴。我看到了一些示例,但我无法修改其中一个以适合我的用例。。;在这里,我用制服替换了我的数据,这样绘图就不会有趣了,但不管怎样

import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
from matplotlib import cm

d = 4
n1 = 100000
n2 = 100

background_data = np.random.uniform(size=(n1,d))
foreground_data = np.random.uniform(size=(n2,d))

fig = plt.figure()

for i in np.arange(d):
    for j in np.arange(d):
        if i != j:
            ax = fig.add_subplot(d,d,1+i*d+j)
            ax = plt.hist2d(background_data[:, i], background_data[:, j],
                       bins=3*n2,
                       cmap=cm.get_cmap('Greys'),
                       norm=mpl.colors.LogNorm())
            ax = plt.plot(foreground_data[:,i],foreground_data[:,j],'o',markersize=0.2)

问:如何共享所有绘图的x轴和y轴

到目前为止,最简单的选择是使用plt.subplot的sharex和sharey参数


到目前为止,最简单的选择是使用plt.subplot的sharex和sharey参数


这太完美了。多谢了,这太完美了。谢谢
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl

d = 4
n1 = 100000
n2 = 100

background_data = np.random.uniform(size=(n1,d))
foreground_data = np.random.uniform(size=(n2,d))

fig, axs = plt.subplots(d,d, sharex=True, sharey=True)

for i in np.arange(d):
    for j in np.arange(d):
        if i != j:
            ax = axs[j,i]
            ax.hist2d(background_data[:, i], background_data[:, j],
                       bins=3*n2,
                       cmap=plt.get_cmap('Greys'),
                       norm=mpl.colors.LogNorm())
            ax.plot(foreground_data[:,i],foreground_data[:,j],'o',markersize=2)
        else:
            axs[j,i].remove()

fig.savefig("sharedaxes.png")            
plt.show()