1

When I'm creating subplots in Python's Maplotlib, I have to do it this way:

fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(nrows=2, ncols=2)

Why, instead, doesn't the following work,

fig, ax1, ax2, ax3, ax4 = plt.subplots(nrows=2, ncols=2)

I'm trying to figure out exactly how plt.subplots. I read the documentation but I am still uncertain as to what is going on. Any help would be appreciated.

Thanks

asked Aug 1, 2015 at 21:53

1 Answer 1

3

plt.subplots() returns a tuple, (fig,axarr), where axarr is an array of axis objects. So if you have two rows and two columns, axarr is a [2,2] array of axes, but the subplots method only will return two objects, not five. This is true for any tuple, and not limited plt.subplots(). So the following will run:

a = 'banana'
b = np.array([1,2,3,4])
tup = (a,b)
fruit,(h,i,j,k) = tup

But fruit,h,i,j,k = tup will not.

answered Aug 1, 2015 at 22:28

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.