0

I'm trying to make a large, multi-panel figure out of several smaller plots using matplotlib.

Usually, I would use plt.subplots to create each individual subplot, however for the data I'm trying to plot at the moment, each panel figure is already made of a many individual subplots (~100), so taking the time to place each one of those subplots in the right place manually for each figure I want to combine into the main plot is too much.

I've been reading the matplotlib documentation, and it seems like I need to create multiple canvas objects in a single window to get what I want. I found this issue on github (https://github.com/matplotlib/matplotlib/issues/6936) asking for a similar feature for a different use, and a similar MEP23 https://matplotlib.org/devel/MEP/MEP23.html. These both seem unresolved/not available.

Seems this may not be possible, or I'm just missing a way to do this. Either way, would be useful to know.

Example:

The code below generates an example of the type of figure I want to create and have several of these in a single plot.

import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns

x = np.linspace(0, 1, 100)
y = np.sin(2*np.pi*x)

fig, ax = plt.subplots(nrows=100)

for axis in ax:
    axis.plot(x, y)
    axis.set(yticks=[],
             xticks=[])
    sns.despine(ax=axis)

fig.text(0.5, 0.05, "x title", ha='center')
fig.text(0.05, 0.5, "y title", va='center', rotation='vertical')
fig.text(0.5, 0.9, "panel title", ha='center')


fig.subplots_adjust(hspace=0)

plt.show()

Current output of code

Desired output of code

  • What's wrong about `plt.subplots(nrows=A, ncols=B)`? – Thomas Kühn Dec 17 '18 at 13:13
  • Maybe you want to have a look at [this answer](https://stackoverflow.com/a/46906599/2454357). – Thomas Kühn Dec 17 '18 at 13:18
  • That works for two side by side, by I was aiming for a 4 panel figure and makes separating two parts one above each other tricky. It also messes with how I'm labelling all my axes at the moment but I can deal with that if I have to. – Angus Fisk Dec 17 '18 at 13:20
  • My main point here maybe is that it is not terribly clear what you are trying to do. Can you maybe be a bit more elaborate (be it with code or by some other means)? – Thomas Kühn Dec 17 '18 at 13:22
  • Sure thing, I've edited the question and created a picture of what I want to achieve, hope that makes sense. – Angus Fisk Dec 17 '18 at 13:33
  • 2
    It looks like you want to use [`GridSpecFromSubplotSpec`](https://matplotlib.org/api/_as_gen/matplotlib.gridspec.GridSpecFromSubplotSpec.html). There are a few examples for that below the docstring. – ImportanceOfBeingErnest Dec 17 '18 at 13:35
  • Yep that looks really useful. Was hoping for some other way around to avoid re-writing a few things but that looks like the way to go. Thanks – Angus Fisk Dec 17 '18 at 14:09

0 Answers0