Skip to content

Instantly share code, notes, and snippets.

@alinaselega
Created January 19, 2024 21:18
Show Gist options
  • Save alinaselega/96d29026628a9e1e2fef418fb670bd04 to your computer and use it in GitHub Desktop.
Save alinaselega/96d29026628a9e1e2fef418fb670bd04 to your computer and use it in GitHub Desktop.
Example of how to make a plt plot with multiple aligned subplots
# from https://matplotlib.org/stable/gallery/subplots_axes_and_figures/gridspec_multicolumn.html
import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec
def format_axes(fig):
for i, ax in enumerate(fig.axes):
ax.text(0.5, 0.5, "ax%d" % (i+1), va="center", ha="center")
ax.tick_params(labelbottom=False, labelleft=False)
fig = plt.figure(layout="constrained")
gs = GridSpec(3, 3, figure=fig)
ax1 = fig.add_subplot(gs[0, :])
ax2 = fig.add_subplot(gs[1, :-1])
ax3 = fig.add_subplot(gs[1:, -1])
ax4 = fig.add_subplot(gs[-1, 0])
ax5 = fig.add_subplot(gs[-1, -2])
fig.suptitle("GridSpec")
format_axes(fig)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment