Alexander Hamilton
Alexander Hamilton

Reputation: 429

How to nest subplots hierchically in pylab matplotlib python?

Concept: Suppose I have some function that creates some arrangement of multiple subplots. Now I want to make a grid of these.

Thus one has a set of subplot areas into which one wants draw a graph which itself has multiple subplots.

Solving this: The way I can do this if I knew ahead of time the whole layout I could with patience figure out how to specify the each plots absolute position and size. Then I could lay this out in absolute coordinates using the subplot2grid() command.

Problem: YUCK! when you write a function to make a fancy graph with subplots, you may be just using this as is. Then someday you want to have a grid of these. You don't want to go back and re-write the original one to now work on a grid. And if you edit that original function you don't want to have to rethink how to cast it into a grid all over.

What you want: logically it would be super delightful if subplots were nestable. so you want something like this. but this doesn't exist.

for i in range (1,4):
  foo = subplot (1,3,i)  # specify a region in your grid

  for j in range(1,5):
      nested_subplot(foo,4,1,j) # now subdivide the specifed region into a subplot
      plot( whatever)

I'm thinking there either must exist some syntax that I don't know about or there's a clever trick one can do to write a function like nested_subplot(foo,4,1,nested)

But as far as I know, subplot simply subdivides a figure. It doesn't subdivide some specified section of a figure. Or am I wrong?

Seems like there ought to be some way to have either subfigures within a figure or subplots within a subplot's cell.

Upvotes: 0

Views: 365

Answers (1)

Jody Klymak
Jody Klymak

Reputation: 5913

If you are brave, you can use matplotlib master and then have access to subfigures: https://matplotlib.org/devdocs/gallery/subplots_axes_and_figures/subfigures.html.

If you need to use a released version you can nest subgridspecs https://matplotlib.org/tutorials/intermediate/gridspec.html

Upvotes: 1

Related Questions