如何使用来自 csv 的 matplotlib 创建饼图

How to create a pie chart using matplotlib from csv

试图按照此处的教程和答案进行操作,但无法全神贯注地根据 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()

我将通过阅读 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()

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

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