如何使用csv中的matplotlib创建饼图

时间:2019-05-15 15:04:02

标签: python csv matplotlib pie-chart

试图按照此处的教程和答案进行操作,但是无法根据我从csv中获得的数据来创建饼图。下面的csv示例

    post_id post_title  subreddit   polarity    subjectivity sentiment
0   bo7h4z  ['league']  soccer       -0.2             0.4    negative
1   bnvieg  ['césar']   soccer         0               0     neutral
2   bnup5q  ['foul']    soccer        0.1             0.6    positive
3   bnul4u  ['benfica'] soccer        0.45            0.5    positive
4   bnthuf  ['prediction']  soccer     0               0     neutral
5   bnolhc  ['revolution' ] soccer     0               0     neutral

还有更多行,但我需要绘制情感专栏,基本上是多少行是积极的,中立的或消极的

outfile = open("clean_soccer.csv","r", encoding='utf-8')
file=csv.reader(outfile)
next(file, None)

post_id = []
post_title = []
subreddit = []
polarity =[]
subjectivity = []
sentiment = []

for row in file:
    post_id.append(row[0])
    post_title.append(row[1])
    subreddit.append(row[2])
    polarity.append(row[3])
    subjectivity.append(row[4])
    sentiment.append(row[5])

plt.pie( , labels=)
plt.axis('equal') 
plt.show()

会是这样吗?

编辑:找到解决方案

import pandas as pd
import matplotlib.pyplot as plt
import csv

df = pd.read_csv('clean_soccer.csv')
df['sentiment'].value_counts().plot.pie()

plt.show()

1 个答案:

答案 0 :(得分:1)

仅通过阅读sentiment列,我将提供一个简短的答案。您需要split才能使用索引[5]访问情感列。然后,您可以使用Counter计算频率,然后使用这些值在饼图中绘制百分比。

import csv
from collections import Counter

outfile = open("clean_soccer.csv","r", encoding='utf-8')
file=csv.reader(outfile)
next(file, None)

sentiment = []

for row in file:
    sentiment.append(row[0].split()[5])

counts = Counter(sentiment[:-1])
plt.pie(counts.values(), labels=counts.keys(), autopct='%1.1f%%',)
plt.axis('equal')
plt.show()

enter image description here

编辑:在下面的评论中回答第二个问题

df['sentiment'].value_counts().plot.pie(autopct='%1.1f%%',)
plt.axis('equal')
plt.show()