有关汽车性能的数据,例如:
Model Running Rest
1 10 14
1 11 13
1 12 12
2 9 15
2 10 14
如何在此处绘制Running
和Rest
每个值的图,以及由于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))
看到关于stackoverflow的几个答案后,我的操作如下:
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
此代码为我提供了Running
和Rest
的每一行值,作为饼图。
但是如何获得说Model
列值相同的前三行的饼图?