3
$\begingroup$

I have written this code fig, (axis1, axis2,axis3, axis4)=plt.subplots(2,2,figsize=(10,4)) and I am getting this error ValueError: not enough values to unpack (expected 4, got 2) I tried many ways to remove this error but all was in vain. Can you explain to me why I am getting this error?

$\endgroup$
  • $\begingroup$ try: x = plt.subplots(2,2,figsize=(10,4)); print(x). what does it say? $\endgroup$ – Stephen Rauch Jun 20 '18 at 1:58
4
$\begingroup$

Its because you have not looked how the values are packed in plt.subplot function.

>>> plt.subplots(2,2,figsize=(10,4))
(<matplotlib.figure.Figure at 0xa3918d0>, array([[<matplotlib.axes._subplots.AxesSubplot object at 0x000000000A389470>,
     <matplotlib.axes._subplots.AxesSubplot object at 0x000000000A41AD30>],
    [<matplotlib.axes._subplots.AxesSubplot object at 0x000000000A6F7EB8>,
     <matplotlib.axes._subplots.AxesSubplot object at 0x000000000BC232E8>]], dtype=object))

Instead of unpacking all values at once, unpack in steps. You will get a better idea then.

For your solution, to unpack -

>>> fig, [[axis1, axis2],[axis3, axis4]] = plt.subplots(2,2,figsize=(10,4))
| improve this answer | |
$\endgroup$
1
$\begingroup$

The second return value will be a 2x2 array. Try this.

fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2,2,figsize=(10,4))
| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.