使用 MatPlotLib,如何将自动缩放的轴从一个图形应用到另一个图形?

With MatPlotLib, how do I apply autoscaled axes from one graph to a separate graph?

所以我有一个要绘制的数据集。在这种情况下,我想在同一个图表上绘制所有数据,然后在其自己的图表上绘制集合中的每个点,但保持每个图表的轴 scale/limits 相同。

所以我需要做的是找到为整组数据设置的自动缩放轴限制的值,并将这些限制应用于每个单独点的图形。

我正在并且一直在阅读 mpl 文档,看看是否有任何我可以使用的函数可以 return 轴限制值,但到目前为止我还没有找到任何东西。

我正在使用 Python 3.4 和 matplotlib

谢谢, evamvid

虽然可以找到

的限制
 xmin, xmax = ax.get_xlim()
 ymin, ymax = ax.get_ylim()

并使用

将它们设置在另一个轴上
ax2.set_xlim(xmin, xmax)
ax2.set_ylim(ymin, ymax)

plt.subplotssharex=Truesharey=True 一起使用可能更容易:

import numpy as np
import matplotlib.pyplot as plt
np.random.seed(2015)

N = 5
x, y = np.random.randint(100, size=(2,N))

fig, axs = plt.subplots(nrows=2, ncols=3, sharex=True, sharey=True)
colors = np.linspace(0, 1, N)
axs[0,0].scatter(x,y, s=200, c=colors)
for i, ax in enumerate(axs.ravel()[1:]):
    ax.scatter(x[i], y[i], s=200, c=colors[i], vmin=0, vmax=1)
plt.show()


另一种选择是pass an axes to sharex and sharey:

ax3 = subplot(313,  sharex=ax1, sharey=ax1)

例如,

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import itertools as IT
np.random.seed(2015)

N = 6
x, y = np.random.randint(100, size=(2,N))
colors = np.linspace(0, 1, N)
gs = gridspec.GridSpec(4, 2)
ax = plt.subplot(gs[0, :])
ax.scatter(x, y, s=200, c=colors)

for k, coord in enumerate(IT.product(range(1,4), range(2))):
    i, j = coord
    ax = plt.subplot(gs[i, j], sharex=ax, sharey=ax)
    ax.scatter(x[k], y[k], s=200, c=colors[k], vmin=0, vmax=1)
plt.tight_layout()
plt.show()