【问题标题】:How to visualize data row wise as a pie chart ? [duplicate]如何将数据行可视化为饼图? [复制]
【发布时间】:2019-01-06 16:14:18
【问题描述】:

如果数据如下:如何为每一行逐行绘制数据表示:

Name Maths Science
John   87     78
Marry  76     68
Harry  98     94

在这里,我想得到每个人在考试行中表现的饼图。

此代码只为第一行绘制饼图,如何获取每行的饼图?

fig, ax = plt.subplots(1,1)    
df.iloc[0].plot(kind='pie', subplots=True, autopct='%1.1f%%')

【问题讨论】:

  • 你运行的是什么python版本?
  • python 2.7 @Yuca

标签: python pandas matplotlib


【解决方案1】:

问题中的问题是您使用.iloc[0] 仅对第一行进行切片。

如果您想绘制所有内容,则需要将其删除(我认为这会更好)或代替 0put :

但是,如果您想对第 0 行到第 5 行进行切片,您需要:

df.iloc[0:5].plot(kind='pie', subplots=True, autopct='%1.1f%%')

【讨论】:

    【解决方案2】:

    正如原始问题上的链接所建议的那样,只需转换您的数据框

    df1.T.plot.pie(subplots=True, figsize=(10, 3))
    

    【讨论】:

    • 如何绘制仅表示row 0 to row 5 的行范围...如何使用loc[] 获取饼图? @尤卡
    • 正如你所说,df.iloc[:6,:].T.plot.pie (... )
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-01
    • 2018-11-27
    • 2023-03-25
    • 2019-10-19
    • 2020-01-30
    • 1970-01-01
    相关资源
    最近更新 更多