目录
1.绘制词云图
2.绘制桑基图
3.绘制平行坐标图
4.绘制结点图
5.绘制地图
本文主要介绍了如何利用Pyecharts绘制词云图、桑基图、平行坐标图、节点图和地图,虽然这些图平时不是很常用,但是看起来还是比较好看的,如果放在论文当中,相信可以让论文更上一层楼。
Pyecharts使用WordCloud绘制词云图。
from pyecharts import options as opts
from pyecharts.charts import Page, WordCloud
from pyecharts.globals import SymbolType
words = [
("牛肉面", 7800),("黄河", 6181),
("《读者》杂志", 4386), ("甜胚子", 3055),
("甘肃省博物馆", 2055),("莫高窟", 8067),("兰州大学", 4244),
("西北师范大学", 1868),("中山桥", 3484),
("月牙泉", 1112),("五泉山", 980),
("五彩丹霞", 865),("黄河母亲", 847),("崆峒山",678),
("羊皮筏子", 1582),("兴隆山",868),
("兰州交通大学", 1555),("白塔山", 2550),("五泉山", 2550)]
c = WordCloud()
c.add("", words, word_size_range=[20, 80])
c.set_global_opts(title_opts=opts.TitleOpts(title="WordCloud-基本示例"))
c.render_notebook()
结果图:
桑基图也称为桑基流图或桑基能量图,是一种用于可视化流量、转移或关系的图表类型。它主要由节点(node)和边(link)组成,节点代表一个实体或者一组实体,边表示在节点间的流动或转移。桑基图在展示数据流向和比例的同时,能够清晰地呈现各个节点之间的关系和互动。
from pyecharts import options as opts
from pyecharts.charts import Sankey
# 节点数据
nodes = [
{'name': '男生'},
{'name': '女生'},
{'name': '打游戏'},
{'name': '加班'},
{'name': '追剧'},
]
# 边数据
links = [
{"source": '男生', "target": '打游戏', "value": 30},
{"source": '男生', "target": '加班', "value": 20},
{"source": '女生', "target": '打游戏', "value": 40},
{"source": '女生', "target": '加班', "value": 15},
{"source": '女生', "target": '追剧', "value": 25},
]
sankey = (
Sankey()
.add(
"",
nodes,
links,
linestyle_opt=opts.LineStyleOpts(opacity=0.2, curve=0.5, color="source"),
label_opts=opts.LabelOpts(position="right"),
node_gap=25
)
.set_global_opts(
title_opts=opts.TitleOpts(title="男生女生兴趣分布"),
tooltip_opts=opts.TooltipOpts(trigger="item", trigger_on="mousemove"),
)
)
sankey.render_notebook()
结果图:
平行坐标图(Parallel Coordinates Plot)是一种多维数据可视化方法,用于可视化具有多个数值型变量的数据集。它通过在平行的直线上绘制多个坐标轴,每个轴代表一个变量,将每个数据点映射到这些坐标轴上的相应位置,以展示多个变量之间的关系和趋势。
from pyecharts import options as opts
from pyecharts.charts import Parallel
import numpy as np
import seaborn as sns
data=sns.load_dataset('iris')
data1=np.array(df[['sepal_length','sepal_width','petal_length','petal_width']]).tolist()
parallel_axis=[{"dim":0,"name":"萼片长度"},
{"dim":1,"name":"萼片宽度"},
{"dim":2,"name":"花瓣长度"},
{"dim":3,"name":"花瓣宽度"},
]
parallel=Parallel(init_opts=opts.InitOpts(width='600px',height='400px'))
parallel.add_schema(schema=parallel_axis)
parallel.add('iris平行坐标图',data=data1,linestyle_opts=opts.LineStyleOpts(width=4,opacity=0.5))
parallel.render_notebook()
结果图:
节点图(Node Link Diagram),也被称为网络图(Network Diagram)或关系图(Graph),是一种用于可视化节点(也称为顶点)和它们之间连接(也称为边)的图表。节点图常用于表示复杂的关系、网络或系统。在节点图中,节点表示实体或对象,如人、地点、物品等,而连接则表示节点之间的关系或连接方式。连接可以是有向的或无向的,具体取决于节点间的关系性质。
from pyecharts import options as opts
from pyecharts.charts import Graph
nodes = [
{"name": "A"},
{"name": "B"},
{"name": "C"},
{"name": "D"},
{"name": "E"},
{"name": "F"},
]
links = []
for i in range(len(nodes)):
for j in range(i+1, len(nodes)):
links.append({"source": nodes[i]["name"], "target": nodes[j]["name"]})
graph = (
Graph()
.add("", nodes, links, repulsion=800, layout="force", is_draggable=True)
.set_global_opts(title_opts=opts.TitleOpts(title="Relationship Graph"))
.render("relationship_graph.html")
)
结果图:
绘制全国主要城市航班流动图
from pyecharts import options as opts
from pyecharts.charts import Geo
from pyecharts.globals import ChartType, SymbolType
c = (
Geo()
.add_schema(maptype="china")
.add( "",
[ ("哈尔滨", 66), ("重庆", 88), ("上海", 100), ("乌鲁木齐", 30),("北京", 30),("兰州",170)],
type_=ChartType.EFFECT_SCATTER,
color="green",
)
.add(
"geo",
[("北京", "兰州"),( "兰州","北京"), ("重庆", "杭州"),("哈尔滨", "重庆"),("乌鲁木齐", "哈尔滨")],
type_=ChartType.LINES,
effect_opts=opts.EffectOpts(
symbol=SymbolType.ARROW, symbol_size=6, color="blue"
),
linestyle_opts=opts.LineStyleOpts(curve=0.2),
)
.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
.set_global_opts(title_opts=opts.TitleOpts(title="主要城市航班路线和数量"))
)
c.render_notebook()
结果图: