关于词云可视化笔记五(tf-idf可视化之河流图)

2022-03-11 13:15:06 浏览数 (1)

河流图能够动态的直观的反映出多个指标随着时序的变化而变化。其实在pyecharts中也提供了ThemeRiver图表,后文会继续讲解;seaborn中也提供了类似的river图,不过效果不是很理想;matplotlib中提供了stackplot图表,baseline要指定为“wiggle”,不过是点与点的直线,比较生硬;后查询了很多材料,需要通过scipy的spline进行插值法处理,经过几天的反复测试,今天终于完全搞定了。

代码示例

代码语言:javascript复制
# coding:utf-8
import pylab
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import cm
from numpy import matrix
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfTransformer
from scipy.interpolate import spline
#文本词频可视化图表stackplot风格
# streamgraph风格的在beaborn上也有,不过不太符合要求
# streamgraph风格的在pyechart上也有,可以直接使用,下次再讲用法
# streamgraph风格的在matplotlib上只有类stackplot,不够圆滑

def draw_river(data,xlabels,ylabels,title='',step=300):
    # X标签 行,即章节
    # Y标签 列,即词汇
    # 数据 即词频,需要转置后才能应用
    #获取y轴数量
    ylen=len(ylabels)
    #初始化一个X轴的序列numpy数组,默认为[0 1 2 len(xlabel)]
    initX = np.array(range(len(xlabels)))
    #linspace用于创建一个是等差数列的一维数组,最小值是0,最大值是X轴长度,
    xnew=np.linspace(initX.min(), initX.max(), step)
    #创建一个numpy空的二维数组newdata,以便存储转换后的data值
    newdata=np.empty(shape=[0,step])
    #spline只能应用于一维数组,所需需要分行读取
    for datarow in range(ylen):
        power_smooth = spline(initX, data[datarow], xnew)
        #将一维numpy数组变为二维数据
        middata = power_smooth[np.newaxis, :]
        #将二维数组添加到最终的数组中
        newdata=np.append(newdata,middata,axis=0)
    pylab.mpl.rcParams['font.sans-serif'] = ['SimHei']  # 防止中文乱码
    pylab.mpl.rcParams['axes.unicode_minus'] = False  # 防止中文乱码
    fig, ax = plt.subplots()
    #用stackplot绘制新的图形
    ax.stackplot(xnew, newdata,labels=ylabels, baseline='wiggle')
    ax.axes.set_yticks(range(len(ylabels)))
    ax.axes.set_yticklabels(ylabels)
    ax.axes.set_xticks(range(len(xlabels)))
    ax.axes.set_xticklabels(xlabels)
    ax.legend(loc='best')
    ax.set_title(title)
    plt.show()

def draw_stackplot(data,xlabels,ylabels):
    # X标签 行,即章节
    # Y标签 列,即词汇
    # 数据 即词频,需要转置后才能应用
    #data= [[0, 3, 3, 3, 0, 0, 3, 0, 3], [0, 3, 0, 3, 0, 6, 3, 0, 3], [3, 0, 0, 0, 3, 0, 3, 3, 0], [0, 3, 3, 3, 0, 0, 3, 0, 3]]
    #xlablels= range(0, 4)
    #ylablels= ['and', 'document', 'first', 'is', 'one', 'second', 'the', 'third', 'this']
    pylab.mpl.rcParams['font.sans-serif'] = ['SimHei']  # 防止中文乱码
    pylab.mpl.rcParams['axes.unicode_minus'] = False  # 防止中文乱码
    fig, ax = plt.subplots()
    ax.stackplot(xlabels, data, labels=ylabels, baseline='wiggle')
    ax.axes.set_yticks(range(len(ylabels)))
    ax.axes.set_yticklabels(ylabels)
    ax.axes.set_xticks(range(len(xlabels)))
    ax.axes.set_xticklabels(xlabels)
    ax.legend(loc='best')
    ax.set_title('Interesting GraphnCheck it out')
    plt.show()

#文本词频可视化图表heatmap风格
def draw_heatmap(data, xlabels, ylabels):
    pylab.mpl.rcParams['font.sans-serif'] = ['SimHei']  # 防止中文乱码
    pylab.mpl.rcParams['axes.unicode_minus'] = False  # 防止中文乱码
    vmin=np.amin(matrix(data))
    vmax = np.amax(matrix(data))
    cmap = cm.Blues
    figure = plt.figure(facecolor='w')
    ax = figure.add_subplot(2, 1, 1, position=[0.1, 0.15, 0.8, 0.8])
    ax.set_yticks(range(len(ylabels)))
    ax.set_yticklabels(ylabels)
    ax.set_xticks(range(len(xlabels)))
    ax.set_xticklabels(xlabels)
    map = ax.imshow(data, interpolation='nearest', cmap=cmap, aspect='auto', vmin=vmin, vmax=vmax)
    cb = plt.colorbar(mappable=map, cax=None, ax=None, shrink=0.5)
    plt.xticks(rotation=90)  # 将字体进行旋转
    plt.yticks(rotation=360)
    plt.show()

#------------------------------英文tf-idf-------------------------------
#英文文本
corpus = [
    'This is the first document.'*3,
    'This is the second second document.'*3,
    'And the third one.'*3,
    'Is this the first document?'*3,
]
# -------------------------词频分析---------------------------
#将文本中的词语转换为词频矩阵
vectorizer = CountVectorizer()
#计算个词语出现的次数
X = vectorizer.fit_transform(corpus)
#X格式如下,主要包括(行 词)词频
#(0, 1)    1     (0, 2)   1     (0, 6)   1     (0, 3)   1     (0, 8)   1     (1, 5)   2     (1, 1)   1
#获取语句中所有文本关键词
word = vectorizer.get_feature_names()
# word格式如下,是个英文词汇的数组列表
# ['and', 'document', 'first', 'is', 'one', 'second', 'the', 'third', 'this']
#查看词频结果,转置为Numpy 2维数组后的输出
print('X.toarray()=',X.toarray())
#and   document   first  is one    second the    third  this
#0 1  1  1  0  0  1  0  1
#0 1  0  1  0  2  1  0  1
#1 0  0  0  1  0  1  1  0
#0 1  1  1  0  0  1  0  1
# ---------------------------可视化----------------------------
#热力图方式
xlabels=word
ylabels=list(range(len(corpus)))
data=X.toarray().tolist()
draw_heatmap(data, xlabels, ylabels)

#转置维stackflow的格式要求,y轴为字符,x轴为章节
#stackplt方式
data=X.T.toarray().tolist()
draw_stackplot(data, ylabels, xlabels)
draw_river(data,ylabels,xlabels,title='词云河流图',step=300)

0 人点赞