【问题标题】:Pie chart with non labelled data?带有未标记数据的饼图?
【发布时间】:2020-09-18 03:08:56
【问题描述】:

当数据没有像往常一样标记时,如何生成饼图?请参阅下面的示例数据框:

             First_Half Second_Half
Div
Bundesliga      0.438       0.562 
EPL             0.434       0.566
La Liga         0.441       0.559

这仅显示列出的分区的上半场进球与下半场进球的比例。我想为每个部门创建一个单独的饼图,其中包含上半场与下半场目标的比例,我该如何处理这些数据?

【问题讨论】:

    标签: python pandas matplotlib seaborn


    【解决方案1】:

    你可以这样做:

    import pandas as pd
    import matplotlib.pyplot as plt
    
    
    df = pd.DataFrame({"Bundesliga":[0.438, 0.562],
                      "EPL": [0.434, 0.566],
                      "La Liga": [0.441, 0.559]}, index=["1st_Half", "2nd_Half"])
    
    df.plot.pie(subplots=True, , figsize=(10, 5), legend=False,
                title="Goals Ratio between first and second halves")
    plt.show()
    

    这将产生这个图表:

    【讨论】:

    • 使用 OP 的原始数据框,您可以转置它:df.T.plot(kind='pie', subplots=True, ...)
    【解决方案2】:

    根据您的数据框,您还可以执行以下操作:

    import pandas as pd
    import matplotlib.pyplot as plt
    
    df = # your data
    
    for i in range(len(df)):
        df.iloc[i].plot.pie()
        plt.show()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-13
      相关资源
      最近更新 更多