Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

subplots in matplotlib give ValueError: not enough values to unpack

The below code gives an error:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-14-ea5c06641335> in <module>()
     14 values = usa.loc[: , "GDP Billions"]
     15 
---> 16 fig, (ax1, ax2, ax3, ax4) = plt.subplots(2, 2, figsize=(15, 6))
     17 
     18 fig.suptitle('GDP Growth', fontsize=20)

ValueError: not enough values to unpack (expected 4, got 2)

If I change fig, (ax1, ax2, ax3, ax4) = plt.subplots(2, 2, figsize=(15, 6)) to fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(15, 6)) and delete the corresponding code for ax3 and ax4 below, it works as desired. Not sure why it is not working as written.

    %matplotlib inline
    import matplotlib.pyplot as plt
    import matplotlib.ticker
    import numpy as np
    from numpy import array

    plt.style.use('seaborn-white')

    plt.rc('ytick', labelsize=14) 
    plt.rc('xtick', labelsize=14) 

    # Plot GDP/Year
    names =  usa.loc[: , "Year"]
    values = usa.loc[: , "GDP Billions"]

    fig, (ax1, ax2, ax3, ax4) = plt.subplots(2, 2, figsize=(15, 6))

    fig.suptitle('GDP Growth', fontsize=20)

    ax1.plot(names, values)
    ax1.xaxis.set_ticks(np.arange(0, 57, 8))
    ax1.set_ylabel('GDP', fontsize=16)
    ax1.set_title('United States',fontsize=16)
    ax1.get_yaxis().set_major_formatter(matplotlib.ticker.FuncFormatter(lambda x, p: format(int(x), ',')))

    ax2.plot(names, values)
    ax2.xaxis.set_ticks(np.arange(0, 57, 8))
    ax2.set_ylabel('Year', fontsize=16)
    ax2.set_title('China',fontsize=16)
    ax2.get_yaxis().set_major_formatter(matplotlib.ticker.FuncFormatter(lambda x, p: format(int(x), ',')))

    ax3.plot(names, values)
    ax3.xaxis.set_ticks(np.arange(0, 57, 8))
    ax3.set_ylabel('GDP', fontsize=16)
    ax3.set_title('United States',fontsize=16)
    ax3.get_yaxis().set_major_formatter(matplotlib.ticker.FuncFormatter(lambda x, p: format(int(x), ',')))

    ax4.plot(names, values)
    ax4.xaxis.set_ticks(np.arange(0, 57, 8))
    ax4.set_ylabel('Year', fontsize=16)
    ax4.set_title('China',fontsize=16)
    ax4.get_yaxis().set_major_formatter(matplotlib.ticker.FuncFormatter(lambda x, p: format(int(x), ',')))

    plt.show()
like image 538
user3504322 Avatar asked Aug 07 '18 22:08

user3504322


1 Answers

plt.subplots() will give you a two-dimensional array of axes (2x2 in your case), hence you need to set this up as follows:

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

Alternatively, you can also use:

fig, axes = plt.subplots(
                     ncols=2,
                     nrows=2,
                     figsize=(15, 6))

ax1, ax2, ax3, ax4 = axes.flatten()
like image 176
Daniel Lenz Avatar answered Nov 08 '22 13:11

Daniel Lenz