python - matplotlib 中的子图给出 ValueError : not enough values to unpack

标签 python matplotlib

下面的代码出现错误:

---------------------------------------------------------------------------
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)

如果我将 Fig, (ax1, ax2, ax3, ax4) = plt.subplots(2, 2, Figsize=(15, 6)) 更改为 fig, (ax1, ax2) = plt.subplots(1, 2, Figsize=(15, 6)) 并删除下面的 ax3ax4 对应代码,就可以了如预期的。不知道为什么它不能按写的那样工作。

    %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()

最佳答案

plt.subplots()将为您提供一个二维轴数组(在您的情况下为 2x2),因此您需要按如下方式进行设置:

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

或者,您也可以使用:

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

ax1, ax2, ax3, ax4 = axes.flatten()

关于python - matplotlib 中的子图给出 ValueError : not enough values to unpack,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51736396/

相关文章:

python - 为什么 Geocoder (1.38.1) 输出 None?

python - 根据部分标签更改饼图的颜色(pandas/matplotlib)

numpy - 在保留分辨率的同时保存类似 imshow 的图像

python - 分割刻度标签或包裹刻度标签

python - colorbar 范围 matplotlib python 的问题

python - matplotlib 散点图图例中缺少标签

javascript - 如何在 PyV8 中加载 Nodejs 模块?

python - 有效地将稀疏 (csc) 矩阵列添加到形状正确的密集 numpy 数组中

python - 如何在Python中调用另一个方法中的方法变量

python - 按给定索引在列表列表中搜索