从 csv 数据绘制饼图?

plotting pie chart from csv data?

汽车性能数据如:

 Model  Running  Rest  
 1      10       14   
 1      11       13  
 1      12       12  
 2       9       15  
 2      10       14

如何在此处绘制 RunningRest 每个值图以及第一个三行的情况,因为 Model 不一样?

import pandas as pd
import matplotlib.pyplot as plt  

df = pd.read_csv('performance.csv')

df.pie(x='Running', y='Rest', autopct='%1.1f%%', shadow=True, startangle=140))

在 Whosebug 上看到几个答案后,我继续进行:

import csv as csv
import matplotlib.pyplot as plt

colors = ['r', 'g']

with open('performance.csv') as csvfile:
readCSV = csv.reader(csvfile, delimiter=',')

i = 0

for row in readCSV:
    if i == 0:
        activities = [row[1], row[2]]
        title = row[0]
    else:
        slices = [row[1], row[2]]
        plt.title("Model: " + row[0])  
        plt.pie(slices, labels=activities, colors=colors, startangle=90, autopct='%.1f%%')
        plt.show()

    i += 1

此代码以饼图形式给出了 RunningRest 的每一行值。

但是如何获得第一个三行的饼图,其中 Model 列值相同?

使用您的原始 df,您可以执行以下操作:

In []:
df[['Running', 'Rest']].groupby(df.Model).plot.pie(subplots=True, autopct='%.1f%%')

Out[]: