pyecharts基礎圖表彙總(三國演義可視化)

Draw.py 算是我造的第一個輪子吧,絕大多數可以用,但是如果有問題就改源碼,源碼在這裏呢是吧。

from pyecharts import options as opts
from pyecharts.charts import Bar
from pyecharts.charts import Pie
from pyecharts.charts import Line
from pyecharts.charts import Graph
from pyecharts.charts import WordCloud
from pyecharts.commons.utils import JsCode
from pyecharts.charts import Radar
from pyecharts.charts import PictorialBar
def rinse(lista):
    arrt = []
    value = []
    for i in range(20):
        arrt.append(lista[i][0])
        value.append(lista[i][1])
    return arrt,value
# 餅圖
def drawpie(attr,value,name):
    list1 = [list(z) for z in zip(attr,value)]
    # 圖表初始化配置
    init_opts = opts.InitOpts(page_title=name)

    pie = Pie(init_opts=init_opts)
    # 標題配置
    title = opts.TitleOpts(title=name,
                           pos_left='center')
    # 圖例配置
    legend_opts = opts.LegendOpts(orient="vertical",
                                  pos_top="20%",
                                  pos_left="15%")

    # 工具箱配置
    # feature = opts.ToolBoxFeatureOpts(save_as_image=True, restore=True, data_view=True, data_zoom=True)
    # 工具箱配置
    toolbox_opts = opts.ToolboxOpts(orient="vertical",
                                    pos_top="25%",
                                    pos_right="15%"
                                    )

    pie.set_global_opts(title_opts=title,
                        legend_opts=legend_opts,
                        toolbox_opts=toolbox_opts
                        )
    # 標籤配置項
    pie.add("",
            list1,
            radius=[30, 75],
            center=['50%', '70%'],
            rosetype="area",
            label_opts=opts.LabelOpts(
                position="outside",
                formatter="{b|{b}: }{c}  {per|{d}%}  ",
                background_color="#eee",
                border_color="#aaa",
                border_width=1,
                border_radius=4,
                rich={
                    "a": {"color": "#999", "lineHeight": 22, "align": "center"},
                    "abg": {
                        "backgroundColor": "#e3e3e3",
                        "width": "100%",
                        "align": "right",
                        "height": 22,
                        "borderRadius": [4, 4, 0, 0],
                    },
                    "hr": {
                        "borderColor": "#aaa",
                        "width": "100%",
                        "borderWidth": 0.5,
                        "height": 0,
                    },
                    "b": {"fontSize": 16, "lineHeight": 33},
                    "per": {
                        "color": "#eee",
                        "backgroundColor": "#334455",
                        "padding": [2, 4],
                        "borderRadius": 2,
                    },
                },
            ),

     )

    pie.render('{0}.html'.format(name))
# 趨勢圖
def drawline(arrt,value,name):

    # 圖表初始化配置
    init_opts = opts.InitOpts(page_title=name)

    line = Line(init_opts=init_opts)
    # 標題配置
    title = opts.TitleOpts(title=name,
                           pos_left="10%")
    # 圖例配置
    legend_opts = opts.LegendOpts(orient="horizontal",
                                  pos_top="5%",
                                  pos_right="15%")

    # 工具箱配置
    # feature = opts.ToolBoxFeatureOpts(save_as_image=True, restore=True, data_view=True, data_zoom=True)
    # 工具箱配置
    toolbox_opts = opts.ToolboxOpts(orient="vertical",
                                    pos_bottom="15%",
                                    pos_left="90%",
                                    )

    line.set_global_opts(title_opts=title,
                         legend_opts=legend_opts,
                         toolbox_opts=toolbox_opts,
                         datazoom_opts = opts.DataZoomOpts(orient="vertical"),
                         )
    line.add_xaxis(arrt, )
    line.add_yaxis(name, value, is_smooth=True, linestyle_opts=opts.LineStyleOpts(color="#E83132", width="4"))
    line.render('{0}.html'.format(name))
# 詞雲圖
def drawWordCloud(words,name):
    # 圖表初始化配置
    init_opts = opts.InitOpts(page_title=name)

    wc = WordCloud(init_opts=init_opts)
    # 標題配置
    title = opts.TitleOpts(title=name,
                           pos_left="50%")
    toolbox_opts = opts.ToolboxOpts(orient="vertical",
                                    pos_bottom="40%",
                                    pos_left="90%",
                                    )

    wc.set_global_opts(title_opts=title,
                       toolbox_opts=toolbox_opts,
                       )
    wc.add("",
           words,
           word_size_range=[20, 1000],
           shape="diamond",
           textstyle_opts=opts.TextStyleOpts(font_family="cursive"),
           )
    wc.render("{0}.html".format(name))
# 柱狀圖
def drawbar(arrt,value,name):
    # 圖表初始化配置
    init_opts = opts.InitOpts(page_title = name,
                              height="700px")
    bar = Bar(init_opts=init_opts)
    # 標題配置
    title = opts.TitleOpts(title=name,
                           pos_left='center')
    # 圖例配置
    legend_opts = opts.LegendOpts(
                                  pos_top="5%",
                                  pos_left="15%")
    # 工具箱配置
    # feature = opts.ToolBoxFeatureOpts(save_as_image=True, restore=True, data_view=True, data_zoom=True)
    # 工具箱配置
    toolbox_opts = opts.ToolboxOpts(
                                    pos_top="5%",
                                    pos_right="30%"
                                    )
    bar.set_global_opts(title_opts=title,
                        legend_opts=legend_opts,
                        toolbox_opts=toolbox_opts,
                        # 區域縮放配置項
                        datazoom_opts=opts.DataZoomOpts(),
                        )

    # add_yaxis

    bar.add_xaxis(arrt)
    # 漸變色
    bar.add_yaxis("",
                  value,
                  gap="0%",
                  category_gap="30%",
                  # 自定義顏色
                  itemstyle_opts=opts.ItemStyleOpts(color=JsCode(
                      """new echarts.graphic.LinearGradient(0, 0, 1, 0,
                                              [{
                                                  offset: 0,
                                                  color: 'rgb(39, 117, 182)'
                                              },
                                              {
                                                  offset: 0.5,
                                                  color: 'rgb(147, 181,207)'
                                              },
                                              {
                                                  offset: 1,
                                                  color: 'rgb(35, 118, 183)'
                                              }], false)"""
                  )),
                  )
    bar.set_series_opts(
        label_opts=opts.LabelOpts(is_show=False),
        markline_opts=opts.MarkLineOpts(
            data=[
                opts.MarkLineItem(type_="min", name="最小值"),
                opts.MarkLineItem(type_="max", name="最大值"),
                opts.MarkLineItem(type_="average", name="平均值"),
            ]
        ),

        markpoint_opts=opts.MarkPointOpts(
            data=[
                opts.MarkPointItem(type_="max", name="最大值"),
                opts.MarkPointItem(type_="min", name="最小值"),
                opts.MarkPointItem(type_="average", name="平均值"),
            ]
        ),
    )
    bar.render('{0}.html'.format(name))
# 地圖
def drawGraph(nodes,links,name):
    # 圖表初始化配置
    init_opts = opts.InitOpts(page_title=name,
                              height="700px")
    g = Graph(init_opts=init_opts)
    # 標題配置
    title = opts.TitleOpts(title=name,
                           pos_left='center')
    # 圖例配置
    legend_opts = opts.LegendOpts(
                                    pos_top="5%",
                                    pos_left="15%"
                                    )
    # 工具箱配置
    # 工具箱配置
    toolbox_opts = opts.ToolboxOpts()
    g.set_global_opts(title_opts=title,
                      legend_opts=legend_opts,
                      toolbox_opts=toolbox_opts,
                      )

    g.add("", nodes, links, repulsion=8000,linestyle_opts=opts.LineStyleOpts(curve=0.2),)
    g.render("{0}.html".format(name))
# 雷達圖
def drawRadar(arr, value, valuemax, name):
    radar = Radar(init_opts=opts.InitOpts(width="1280px", height="720px", bg_color="#CCCCCC"))
    v_max = [list(z) for z in zip(arr, valuemax)]
    radar.add_schema(
        schema=[
            opts.RadarIndicatorItem(name=k, max_=v) for k, v in v_max
        ],
        splitarea_opt=opts.SplitAreaOpts(
            is_show=True, areastyle_opts=opts.AreaStyleOpts(opacity=1)
        ),
        textstyle_opts=opts.TextStyleOpts(color="#fff"),
    )
    radar.add(
        series_name=name,
        data=value,
        linestyle_opts=opts.LineStyleOpts(color="#CD0000"),
    )
    radar.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
    radar.set_global_opts(
        title_opts=opts.TitleOpts(title=name), legend_opts=opts.LegendOpts()
    )
    radar.render("{0}.html".format(name))
# arr = ["銷售(sales)", "管理(Administration)", "信息技術(Information Technology)", "客服(Customer Support)",
#        "研發(Development)",
#        "市場(Marketing)"]
# value = [[4300, 10000, 28000, 35000, 50000, 19000]]
# valuemax = [6500, 16000, 30000, 38000, 52000, 25000]
# draw(arr, value, valuemax, "data/清華大學")
# 象形圖
def drawPictorialBar(location,values,name):
    c = (
        PictorialBar()
        .add_xaxis(location)
        .add_yaxis(
            "",
            values,
            label_opts=opts.LabelOpts(is_show=False),
            symbol_size=22,
            symbol_repeat="fixed",
            symbol_offset=[0, -5],
            is_symbol_clip=True,
            # symbol='image://https://github.githubassets.com/images/spinners/octocat-spinner-32.gif'
            symbol='image://http://weizhendong.top/images/1.png'
        )
        .reversal_axis()
        .set_global_opts(
            title_opts=opts.TitleOpts(title="PictorialBar-自定義 Symbol"),
            xaxis_opts=opts.AxisOpts(is_show=False),
            yaxis_opts=opts.AxisOpts(
                axistick_opts=opts.AxisTickOpts(is_show=False),
                axisline_opts=opts.AxisLineOpts(
                    linestyle_opts=opts.LineStyleOpts(opacity=0)
                ),
            ),
        )
        .render("{0}.html".format(name))
    )
# location = ["山西", "四川", "西藏", "北京", "上海", "內蒙古", "雲南", "黑龍江", "廣東", "福建"]
# values = [13, 42, 67, 81, 86, 94, 166, 220, 249, 262]
# drawPictorialBar(location,values,"llalal")

應用:

from collections import Counter
import jieba.posseg as psg
import Draw as draw
import pyecharts.options as opts
def count(seg_list1):
    # 計數
    count = Counter(seg_list1)
    # 字典排序
    result = sorted(count.items(), key=lambda x: x[1], reverse=True)
    return result

def readjieba(text,excludes,list_replace):
    dic_result = {}
    seg_list1 = []
    nr=[]
    ns=[]
    # 分詞
    seg_list = psg.cut(text)
    for w, t in seg_list:
        # 去除停用詞
        if len(w) != 1 and t != 'm' and w not in excludes:
            # 替換詞
            for j in list_replace:
                if w == j[0]:
                    real_word == j[1]
                else:
                    real_word = w
            if t == 'nr':
                nr.append("{0}".format(real_word))
            if t=='ns':
                ns.append("{0}".format(real_word))
            seg_list1.append("{0}".format(real_word))

    dic_result.setdefault("全部", count(seg_list1))
    dic_result.setdefault("人名", count(nr))
    dic_result.setdefault("地名", count(ns))
    return dic_result

def names_relationships(text_list,excludes,list_replace):
    names = {}#  保存人物,鍵爲人物名稱,值爲該人物在全文中出現的次數
    relationships = {}#保存人物關係的有向邊,鍵爲有向邊的起點,值爲一個字典 edge ,edge 的鍵爲有向邊的終點,值是有向邊的權值
    lineNames = []# 緩存變量,保存對每一段分詞得到當前段中出現的人物名稱
    for line in text_list:
        poss = psg.cut(line)  # 分詞,返回詞性
        lineNames.append([])  # 爲本段增加一個人物列表
        for w in poss:
            if w.flag != 'nr' or len(w.word) < 2 or w.word in excludes:
                continue  # 當分詞長度小於2或該詞詞性不爲nr(人名)時認爲該詞不爲人名
            else:
                for j in list_replace:
                    if w.word == j[0]:
                        real_word == j[1]
                    else:
                        real_word = w.word
            lineNames[-1].append(real_word)  # 爲當前段的環境增加一個人物

            if names.get(real_word) is None:  # 如果某人物(w.word)不在人物字典中
                names[real_word] = 0
                relationships[real_word] = {}
            names[real_word] += 1

                # 輸出人物出現次數統計結果
    # for name, times in names.items():
    #    print(name, times)

    # 對於 lineNames 中每一行,我們爲該行中出現的所有人物兩兩相連。如果兩個人物之間尚未有邊建立,則將新建的邊權值設爲 1,
    # 否則將已存在的邊的權值加 1。這種方法將產生很多的冗餘邊,這些冗餘邊將在最後處理。
    for line in lineNames:
        for name1 in line:
            for name2 in line:
                if name1 == name2:
                    continue
                if relationships[name1].get(name2) is None:
                    relationships[name1][name2] = 1
                else:
                    relationships[name1][name2] = relationships[name1][name2] + 1
    return names,relationships

                # 由於分詞的不準確會出現很多不是人名的“人名”,從而導致出現很多冗餘邊,
                # 爲此可設置閾值爲10,即當邊出現10次以上則認爲不是冗餘
# 處理節點和邊
def node_links(names,relationships):
    nodes = []
    links = []
    for name, times in names.items():
        if times > 10:
            nodes.append(opts.GraphNode(name=str(name), symbol_size=times/10,))
    for name, edges in relationships.items():
        for v, w in edges.items():
            if w > 10:
                links.append(opts.GraphLink(source=str(name), target=str(v), value=str(w)))
    return nodes,links

def rinse(lista):
    arrt = []
    value = []
    for i in range(20):
        arrt.append(lista[i][0])
        value.append(lista[i][1])
    return arrt,value


if __name__ == '__main__':
    excludes = {'將軍', '卻說', '令人', '趕來', '徐州', '不見', '下馬', '喊聲', '因此', '未知', '大敗', '百姓', '大事', '一軍', '之後', '接應', '起兵',
                '成都', '原來', '江東', '正是', '忽然', '原來', '大叫', '上馬', '天子', '一面', '太守', '不如', '忽報', '後人', '背後', '先主', '此人',
                '城中', '然後', '大軍', '何不', '先生', '何故', '夫人', '不如', '先鋒', '二人', '不可', '如何', '荊州', '不能', '如此', '主公', '軍士',
                '商議', '引兵', '次日', '大喜', '魏兵', '軍馬', '於是', '東吳', '今日', '左右', '天下', '不敢', '陛下', '人馬', '不知', '都督', '漢中',
                '一人', '衆將', '後主', '只見', '蜀兵', '馬軍', '黃巾', '立功', '白髮', '大吉', '紅旗', '士卒', '錢糧', '於漢', '郎舅', '龍鳳', '古之',
                '白虎', '古人云', '爾乃', '馬飛報', '軒昂', '史官', '侍臣', '列陣', '玉璽', '車駕', '老夫', '伏兵', '都尉', '侍中', '西涼', '安民', '張曰', '文武',
                '白旗',
                '祖宗', '尋思'}  # 排除的詞彙
    key = "玄德曰"
    value = "劉備"
    list_replace = []
    list_replace.append(tuple((key, value)))
    print(list_replace)

    with open('三國.txt', 'r', encoding='utf-8') as f:
        text =f.read()
    result = readjieba(text,excludes,list_replace)




    """詞雲"""
    draw.drawWordCloud(result["全部"],"詞雲")

    lista = result["人名"]
    """柱狀圖,餅圖,折線"""
    arrt, value = rinse(lista)
    draw.drawpie(arrt,value,"餅圖")
    draw.drawbar(arrt,value,"柱狀圖")
    draw.drawline(arrt,value,"折線")

    """關係圖"""
    text_list =text.splitlines()
    names, relationships = names_relationships(text_list, excludes, list_replace)
    nodes, links = node_links(names, relationships)
    draw.drawGraph(nodes, links, "關係圖")

在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章