19

I need to draw subplots of a figure through loop iterations; each iteration calls a function defined in another module (=another py file), which draws a pair of subplots. Here is what I tried -- and alas does not work:

1) Before the loop, create a figure with the adequate number of rows, and 2 columns:

 import matplotlib.pyplot as plt     
 fig, axarr = plt.subplots(nber_rows,2)

2) Inside the loop, at iteration number iter_nber, call on the function drawing each subplot:

 fig, axarr = module.graph_function(fig,axarr,iter_nber,some_parameters, some_data)

3) The function in question is basically like this; each iteration creates a pair of subplots on the same row:

 def graph_function(fig,axarr,iter_nber,some_parameters, some_data):

     axarr[iter_nber,1].plot(--some plotting 1--)
     axarr[iter_nber,2].plot(--some plotting 2--)

     return fig,axarr

This does not work. I end up with an empty figure at the end of the loop. I have tried various combinations of the above, like leaving only axarr in the function's return argument, to no avail. Obviously I do not understand the logic of this figure and its subplots.

Any suggestions much appreciated.

8
  • Could you provide a working example? Commented Dec 19, 2014 at 15:46
  • thanks hitzg. I was hoping that the simplified code would help you see what my problem is. The real code is very very long and full of details that are completely unrelated to the question at hand. Commented Dec 19, 2014 at 15:49
  • 2
    One thing that looks odd ist the index 2 in axarr[iter_nber,2]. that should throw an error Commented Dec 19, 2014 at 15:50
  • I thought that this would be the 2d column of the subplot grid? I get no error at all in fact. Just an empty figure. Commented Dec 19, 2014 at 15:52
  • 1
    @Charles - Numpy arrays (and more or less all python objects) use 0-based indexing. x[2] is the third item in x, not the second. The code you've posted appears essentially correct. The error is probably due to something fairly mundane that's just not shown in the code you posted. Commented Dec 19, 2014 at 15:56

1 Answer 1

23

The code you've posted seems largely correct. Other than the indexing, as @hitzg mentioned, nothing you're doing looks terribly out of the ordinary.

However, it doesn't make much sense to return the figure and axes array from your plotting function. (If you need access to the figure object, you can always get it through ax.figure.) It won't change anything to pass them in and return them, though.

Here's a quick example of the type of thing it sounds like you're trying to do. Maybe it helps clear some confusion?

import numpy as np
import matplotlib.pyplot as plt

def main():
    nrows = 3
    fig, axes = plt.subplots(nrows, 2)

    for row in axes:
        x = np.random.normal(0, 1, 100).cumsum()
        y = np.random.normal(0, 0.5, 100).cumsum()
        plot(row, x, y)

    plt.show()

def plot(axrow, x, y):
    axrow[0].plot(x, color='red')
    axrow[1].plot(y, color='green')

main()

enter image description here

Sign up to request clarification or add additional context in comments.

3 Comments

thanks Joe yes this is useful. I see in particular that your function "plot" returns nothing at all. I will follow this logic. Also, I was wondering if the fact that my plotting function is in another file could have an influence? I don't see why but I may be mistaken. thanks again
@Charles - As long as your plotting function is properly imported from the other file, it shouldn't matter. (E.g. if it's a function called plot in blah.py, then you'll need to do import blah and blah.plot(...), rather than just plot(...).) As far as what to return (or not return), a common convention is to return the artists (e.g. lines, etc) that are plotted. For exmaple, line = ax.plot(...); return line. That's purely optional, though. It's just for potential ease of use later on.
Is it possible to iterate over subplots in this way? Say I generate a figure with 9 subplots in 3x3 format. Now I need to create a chart in each subplot. Do I need to write 9 lines or can I loop over these subplots in some way?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.