记录python3生成词云的方式,方便以后查找使用
from wordcloud import WordCloud
import matplotlib.pyplot as plt
cloud = WordCloud(font_path = '',
background_color = 'white',
max_words = 200,
max_font_size = 40,
collocations = False,
scale = 4) # scale代表图片清晰度
wordcloud = cloud.generate(words)
wordcloud = cloud.generate_from_frequencies(words)
plt.figure(figsize = (20,20))
plt.imshow(wordcloud)
plt.axis('off')
plt.savefig('/wordcloud.jpg')
plt.show()#plt.show一定要在plt.savefig之后运行,否则会保存空白图片
备注:
1)font_path:字体文件存放路径,字体文件可用下载地址:www.font5.com.cn/font_download.php?id=664&part=1246266896
2)wordcloud = cloud.generate(words) 生成词云的方式之一,此方式需要的输入参数words其形式为‘我 我 喜欢 吃 苹果 苹果’,即一个字符串,词与词之间用空格作为连接符
3)wordcloud = cloud.generate_from_frequencies(words) 生成词云的方式之一,此方式需要的输入参数words是一个字典,字典的key是单词,对应的value为词频,例如words = {‘我’:2,‘喜欢’:1,‘吃’:1,‘苹果’:2}
4)collocations 代表是否使用组合词,如果设置为True,则会将组合词作为一个词进行展示
记录matplotlib.pyplot绘制loss曲线的方式,方便以后查找使用
import matplotlib.pyplot as plt
step = []
loss = []#分别存储神经网络训练的step和loss值
plt.plot(step,loss,'r-')#r代表red红色,-代表实线
plt.xlabel('step')
plt.ylabel('loss')
plt.axis([0,10,0,1])#分别代表xmin,xmax,ymin,ymax。需要设置xy轴的最小值和最大值,否则当每次绘图的数值刻度不一致,不方便比较
plt.title('loss-step in epoch')
savepath = '......'
plt.savefig(f'{avepath}/train_loss.jpg')
plt.show()
plt.close()#一定一定要close,否则会将第一张图片的内容打印在第二张图片上!!!!