如何在python中显示wordcloud中每个用户发出的推文的正颜色和负颜色类别
我正在尝试使用情感值基于类别和类别频率计数为用户创建wordcloud,如果情感值为正值,则类别应以绿色显示,而负值应以红色显示如何在python中显示wordcloud中每个用户发出的推文的正颜色和负颜色类别,python,python-3.x,matplotlib,word-cloud,Python,Python 3.x,Matplotlib,Word Cloud,我正在尝试使用情感值基于类别和类别频率计数为用户创建wordcloud,如果情感值为正值,则类别应以绿色显示,而负值应以红色显示 |id |sentiment|category|sentiment_count|category_count|rank| +------------------------------------+---------+--------+---------------+--------------+---
|id |sentiment|category|sentiment_count|category_count|rank|
+------------------------------------+---------+--------+---------------+--------------+----+
|276c08e3-29c6-416a-b522-5b1c7ad0a865|positive |Business|9 |9 |1 |
|276c08e3-29c6-416a-b522-5b1c7ad0a865|negative |Business|1 |1 |2 |
|09eb80df-76fd-4d94-8bb1-f4ffb0ece4c6|negative |World |10 |10 |1 |
|09eb80df-76fd-4d94-8bb1-f4ffb0ece4c6|positive |Business|7 |7 |2 |
|09eb80df-76fd-4d94-8bb1-f4ffb0ece4c6|positive |Sci/Tech|6 |6 |3 |
|09eb80df-76fd-4d94-8bb1-f4ffb0ece4c6|negative |Business|2 |2 |4 |
|09eb80df-76fd-4d94-8bb1-f4ffb0ece4c6|negative |Sci/Tech|1 |1 |5 |
|09eb80df-76fd-4d94-8bb1-f4ffb0ece4c6|positive |Sports |1 |1 |5 |
|09eb80df-76fd-4d94-8bb1-f4ffb0ece4c6|positive |World |1 |1 |5 |
|4f11697e-d3f0-47a0-b1f8-aaacf7b720b7|positive |Sports |12 |12 |1 |
|4f11697e-d3f0-47a0-b1f8-aaacf7b720b7|positive |Sci/Tech|1 |1 |2 |
|f29a2c29-91cf-41d8-a586-453218331710|positive |Sci/Tech|759 |759 |1 |
|f29a2c29-91cf-41d8-a586-453218331710|negative |Business|524 |524 |2 |
|f29a2c29-91cf-41d8-a586-453218331710|positive |Business|508 |508 |3 |
|f29a2c29-91cf-41d8-a586-453218331710|negative |World |279 |279 |4 |
|f29a2c29-91cf-41d8-a586-453218331710|negative |Sci/Tech|220 |220 |5 |
我试过使用代码。但不确定如何根据情绪对颜色进行分类(消极和积极)
@约翰:你有什么解决办法吗?
from wordcloud import WordCloud
import matplotlib.pyplot as plt
from wordcloud import WordCloud
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
source1 = 'path_to_csv'
df1 = pd.read_csv(source1)
print(df1)
uids1 = np.unique(df1['id'])
fig, axes = plt.subplots(nrows=(len(uids)+2)//3, ncols=3, figsize=(20, 8),
gridspec_kw={'hspace': 0.05, 'wspace': 0.05, 'left': 0.01, 'right': 0.99, 'top': 0.99, 'bottom': 0.01})
for id, ax in zip(uids, axes.ravel()):
data = df[df['id'] == id].set_index('category')['category_count'].to_dict()
wc = WordCloud(width=800, height=400, max_words=200).generate_from_frequencies(data)
ax.imshow(wc, interpolation='bilinear')
ax.set_title(f'id = {id}')
ax.axis('off')
plt.savefig("/path/wordcloud_twitte.png", format="png")