Skip to content Skip to sidebar Skip to footer

Plotting Autoscaled Subplots With Fixed Limits In Matplotlib

What's the best way in matplotlib to make a series of subplots that all have the same X and Y scales, but where these are computed based on the min/max ranges of the subplot with t

Solution 1:

Matplotlib/Pyplot: How to zoom subplots together?

http://matplotlib.org/examples/pylab_examples/shared_axis_demo.html

http://matplotlib.org/users/recipes.html

quoting the last link:

Fernando Perez has provided a nice top level method to create in subplots() (note the “s” at the end) everything at once, and turn off x and y sharing for the whole bunch. You can either unpack the axes individually:

# new style method 1; unpack the axes
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, sharex=True, sharey=True)
ax1.plot(x)

or get them back as a numrows x numcolumns object array which supports numpy indexing:

# new style method2; use an axes array
fig, axs = plt.subplots(2, 2, sharex=True, sharey=True)
axs[0,0].plot(x)

If you have an old version of matplotlib the following method should work (also quoting from the last link)

Easily creating subplots In early versions of matplotlib, if you wanted to use the pythonic API and create a figure instance and from that create a grid of subplots, possibly with shared axes, it involved a fair amount of boilerplate code. Eg

# old stylefig = plt.figure()
ax1 = fig.add_subplot(221)
ax2 = fig.add_subplot(222, sharex=ax1, sharey=ax1)
ax3 = fig.add_subplot(223, sharex=ax1, sharey=ax1)
ax3 = fig.add_subplot(224, sharex=ax1, sharey=ax1)

Post a Comment for "Plotting Autoscaled Subplots With Fixed Limits In Matplotlib"