import csv import matplotlib.pyplot as plt def csv_rows_to_label_count(rows, column): """ take a list of csv rows and for a given column return a dictionary of labels mapped to the number of times that label appears """ results = {} # loop through rows # if we are seeing the label for the first time set label to 1 # otherwise increment that label's current count by 1 for row in rows: val = row[column] if val == "": val = "Unknown" if results.has_key(val): results[val] = results[val] + 1 else: results[val] = 1 return results with open("KaranisExcavationRecords.csv") as f: reader = csv.DictReader(f) results = csv_rows_to_label_count(reader, 'ObjectType') fixed_results = results.items() labels = [result[0] for result in fixed_results] sizes = [result[1] for result in fixed_results] plt.pie(sizes, labels=labels, autopct='%1.1f%%', shadow=True, startangle=90) plt.axis('equal') plt.show()