【问题标题】:Multiple charts not working with subplot function? Python多个图表不适用于子图功能? Python
【发布时间】:2021-10-28 19:47:18
【问题描述】:

我创建了 6 个饼图,但是,它们都单独显示。我想让它们都出现在同一个图上(3 行,2 列)。

因此,我尝试使用 subplot 函数,但我认为我没有正确使用它,而且我看不出哪里出错了。

以下是我使用 python 2.7 编写的代码。我是编码新手,只是需要一些指导来解决这个小问题,谢谢!

另外,如果您有任何关于使此代码更“精简”的提示,我会全力以赴!

我得到错误:

AttributeError: 'numpy.ndarray' object has no attribute 'pie'

【问题讨论】:

    标签: python pandas python-2.7 charts subplot


    【解决方案1】:

    让您的代码运行的最快方法是在创建后立即将二维 axs 数组“展平”为一维数组:

    fig, axs = plt.subplots(3, 2)
    axs = axs.flatten()
    # The rest of your existing code
    

    另外,Python 中的索引从 0 开始,因此您需要将索引号从 0 更改为 5,而不是 1 到 6。

    解释:你得到的错误来自fig, axs = plt.subplots(3, 2)返回的轴数组是二维的,有3行2列。所以第一个 AxesSubplot 对象位于 axs[0, 0](或 axs[0][0]),但 axs[0] 返回 3×2 数组的第一行,它本身就是一个数组。

    (另一方面,您应该升级到 Python 3.x :) https://www.python.org/doc/sunset-python-2/

    【讨论】:

      猜你喜欢
      • 2018-04-23
      • 2019-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-08
      • 2020-11-07
      • 1970-01-01
      相关资源
      最近更新 更多