【问题标题】:Matplotlib subplot - chart is overwrittenMatplotlib 子图 - 图表被覆盖
【发布时间】:2021-01-19 12:50:27
【问题描述】:

我想根据以下数据创建两个饼图:

post_type   owner_user_id   year_posted score   count
A           150165022       332372      2567380 35846
Q           112349610       283997       180680 2111

一个由score 组成,另一个由count 分组,由post_type 分组。所以,我正在使用以下代码:

axes[0].labels = ['answers', 'questions']
axes[0].sizes = buffer_df['score']
axes[0].explode = (0, 0.3)
axes[0].pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%',shadow=True, startangle=90)
axes[0].axis('equal')

axes[1].labels = ['answers a', 'questions a']
axes[1].sizes = buffer_df['count']
axes[1].explode = (0, 0.3)
axes[1].pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%',shadow=True, startangle=90)
axes[1].axis('equal')

plt.show()

但我只得到了两次绘制的第一张图表:

我知道这是第一个图表,因为我更改了第二个图表的标签。

谁能告诉我,我做错了什么?

【问题讨论】:

    标签: pandas matplotlib subplot


    【解决方案1】:

    我不知道为什么会这样,但是如果你直接描述它,你可以画出你想要的。

    import matplotlib.pyplot as plt
    
    fig, axes= plt.subplots(1,2, figsize=(12,9))
    
    axes[0].pie(buffer_df['score'], explode=(0, 0.3), labels=['answers', 'questions'], autopct='%1.1f%%',shadow=True, startangle=90)
    axes[0].axis('equal')
    axes[1].pie(buffer_df['count'], explode=(0, 0.3), labels=['answers a', 'questions a'], autopct='%1.1f%%',shadow=True, startangle=90)
    axes[1].axis('equal')
    
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 2015-11-30
      • 1970-01-01
      • 2020-08-06
      • 1970-01-01
      • 2019-03-12
      • 2021-11-24
      • 1970-01-01
      • 2023-02-23
      • 1970-01-01
      相关资源
      最近更新 更多