所以我有一个想要绘制的数据集。在这种情况下,我想将所有数据绘制在同一个图表上,然后将集合中的每个点绘制在其自己的图表上,但保持每个图表的 Axis 比例/限制相同。
所以我需要做的是找到为整组数据设置的自动缩放 Axis 限制的值,并将这些限制应用于每个单独点的图表。
我正在阅读 mpl 文档,看看是否有任何类型的函数可以使用来返回 Axis 限制值,但到目前为止我还没有找到任何东西。
我正在使用 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.subplots
与 sharex=True
和 sharey=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()
关于python - 使用 MatPlotLib,如何将自动缩放 Axis 从一个图形应用到另一个图形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31504569/