python | 数据可视化 词云
python | 数据可视化 词云
直接丢代码:
import matplotlib.pyplot as plt
import matplotlib.colors as colors # 处理图片相关内容
from wordcloud import WordCloud
import jieba
from PIL import Image
import numpy as np
import sqlite3
# 读文件
# text_from_file_with_apath = open('数字爱情故事.md', encoding='utf-8').read()
text_from_file_with_apath = ''
conn = sqlite3.connect('msg.db')
c = conn.cursor()
cursor = c.execute("SELECT * from mz_msg;")
for row in cursor:
text_from_file_with_apath += row[1]
conn.close()
# print(text_from_file_with_apath)
image = np.array(Image.open('1.jpg')) # 词云形状
# 自定义文字颜色
# colormaps = colors.ListedColormap(['#FF0000','#FF7F50','#FFE4C4']) # 红色系
colormaps = colors.ListedColormap(['#0010FF','#00E5FF','#BF46FF']) # 蓝色系
wordlist_after_jieba = jieba.lcut(text_from_file_with_apath, cut_all = True)
# 整理成字符串
wl_space_split = ''
for i in wordlist_after_jieba:
if len(i) > 1:
wl_space_split += i
wl_space_split += " "
# print(wordlist_after_jieba) # 分词字典
# wl_space_split = " ".join(wordlist_after_jieba) # 整理词成字符串
# print(wl_space_split)
# 生成
my_wordcloud = WordCloud(
font_path="a.ttf",
mask=image,
background_color='white', # 指定背景颜色,默认黑色
colormap=colormaps, # 指定文字颜色
width=800, # 指定宽度
height=600, # 指定高度
font_step=4, #字号步长
min_font_size=8,max_font_size=250, #字号
).generate(wl_space_split)
# 显示
plt.imshow(my_wordcloud)
plt.axis("off")
plt.show()
my_wordcloud.to_file('词云1.png') # 生成词云图片
本文来自博客园,作者:Mz1,转载请注明原文链接:https://www.cnblogs.com/Mz1-rc/p/15709166.html
如果有问题可以在下方评论或者email:mzi_mzi@163.com