import matplotlib.pyplot as plt from wordcloud import WordCloud # 读取包含弹幕数据的文本文件 with open('爬取的总弹幕.txt', mode='r', encoding='utf-8') as f: text = f.read() # 生成词云图 wc = WordCloud(font_path='msyh.ttc', # 如果需要支持中文字符,可以指定字体文件路径,例如 'simsun.ttc' width=800, height=400, background_color='white', ).generate(text) wc.to_file('词云图.png') # 显示词云图 plt.figure(figsize=(10, 5)) plt.imshow(wc, interpolation='bilinear') plt.axis('off') # 隐藏坐标轴 plt.show() with open('AI弹幕.txt', mode='r', encoding='utf-8') as f: text = f.read() # 生成词云图 wc = WordCloud(font_path='msyh.ttc', # 如果需要支持中文字符,可以指定字体文件路径,例如 'simsun.ttc' width=800, height=400, background_color='white', ).generate(text) wc.to_file('AI弹幕.png') # 显示词云图 plt.figure(figsize=(10, 5)) plt.imshow(wc, interpolation='bilinear') plt.axis('off') # 隐藏坐标轴 plt.show()