Say that I have a figure fig
which contains two subplots as in the example from the documentation:
I can obtain the two axes (the left one being ax1
and the right one ax2
) by just doing:
ax1, ax2 = fig.axes
Now, is it possible to rearrange the subplots? In this example, to swap them?
Sure, as long as you're not going to use subplots_adjust
(and therefore tight_layout
) after you reposition them (you can use it safely before).
Basically, just do something like:
import matplotlib.pyplot as plt
# Create something similar to your pickled figure......
fig, (ax1, ax2) = plt.subplots(ncols=2)
ax1.plot(range(10), 'r^-')
ax1.set(title='Originally on the left')
ax2.plot(range(10), 'gs-')
ax2.set(title='Originally on the right')
# Now we'll swap their positions after they've been created.
pos1 = ax1.get_position()
ax1.set_position(ax2.get_position())
ax2.set_position(pos1)
plt.show()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With