evamvid
evamvid

Reputation: 861

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

So I have a dataset I want to plot. In this case, I want to plot all the data on the same graph, and then graph each point in the set on its own graph, but keep the axis scale/limits the same for each graph.

So what I need to do is find the values of the autoscaled axis limits that were set for the full set of data, and apply those limits to the graph for each individual points.

I am and have been reading the mpl docs to see if theres any kind of function I can use that will return the axis limits values, but I haven't found anything so far.

Im using Python 3.4 with matplotlib

Thanks, evamvid

Upvotes: 1

Views: 42

Answers (1)

unutbu
unutbu

Reputation: 879481

Although it is possible to find the limits with

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

and set them on another axes with

ax2.set_xlim(xmin, xmax)
ax2.set_ylim(ymin, ymax)

it might be easier to use plt.subplots with sharex=True and 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()

enter image description here


Another option is to pass an axes to sharex and sharey:

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

For example,

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()

enter image description here

Upvotes: 3

Related Questions