实验环境
导入本次实验所需要的模块
这次实验我是在jupyterlab环境上进行的(实时渲染能够很好的观察数据、一个很友好的界面,用着很舒服,非常推荐使用)
from pyecharts.globals import CurrentConfig, NotebookType
CurrentConfig.NOTEBOOK_TYPE = NotebookType.JUPYTER_LAB
import pyecharts.options as opts
from pyecharts.globals import ThemeType
import pandas as pd
字符云
from pyecharts.charts import WordCloud
post_data = pd.read_csv('data/post_data.csv')
post_data2 = post_data.groupby(by=['category']).agg({'views': sum}).reset_index().values
wordcloud = (
WordCloud(init_opts=opts.InitOpts(theme=ThemeType.DARK, bg_color='white'))
.add("", post_data2, word_size_range=[20, 100])
.set_colors('white')
)
# load_javascript()只有第一次使用的时候需要调用,后续不需要再次调用
wordcloud.load_javascript()
wordcloud.render_notebook()
主题河流示意图
from pyecharts.charts import ThemeRiver
datax = ['分支1', '分支2', '分支3', '分支4', '分支5', '分支6']
datay = [
['2015/11/08', 10, '分支1'], ['2015/11/09', 15, '分支1'], ['2015/11/10', 35, '分支1'],
['2015/11/14', 7, '分支1'], ['2015/11/15', 2, '分支1'], ['2015/11/16', 17, '分支1'],
['2015/11/17', 33, '分支1'], ['2015/11/18', 40, '分支1'], ['2015/11/19', 32, '分支1'],
['2015/11/20', 26, '分支1'], ['2015/11/21', 35, '分支1'], ['2015/11/22', 40, '分支1'],
['2015/11/23', 32, '分支1'], ['2015/11/24', 26, '分支1'], ['2015/11/25', 22, '分支1'],
['2015/11/08', 35, '分支2'], ['2015/11/09', 36, '分支2'], ['2015/11/10', 37, '分支2'],
['2015/11/11', 22, '分支2'], ['2015/11/12', 24, '分支2'], ['2015/11/13', 26, '分支2'],
['2015/11/14', 34, '分支2'], ['2015/11/15', 21, '分支2'], ['2015/11/16', 18, '分支2'],
['2015/11/17', 45, '分支2'], ['2015/11/18', 32, '分支2'], ['2015/11/19', 35, '分支2'],
['2015/11/20', 30, '分支2'], ['2015/11/21', 28, '分支2'], ['2015/11/22', 27, '分支2'],
['2015/11/23', 26, '分支2'], ['2015/11/24', 15, '分支2'], ['2015/11/25', 30, '分支2'],
['2015/11/26', 35, '分支2'], ['2015/11/27', 42, '分支2'], ['2015/11/28', 42, '分支2'],
['2015/11/08', 21, '分支3'], ['2015/11/09', 25, '分支3'], ['2015/11/10', 27, '分支3'],
['2015/11/11', 23, '分支3'], ['2015/11/12', 24, '分支3'], ['2015/11/13', 21, '分支3'],
['2015/11/14', 35, '分支3'], ['2015/11/15', 39, '分支3'], ['2015/11/16', 40, '分支3'],
['2015/11/17', 36, '分支3'], ['2015/11/18', 33, '分支3'], ['2015/11/19', 43, '分支3'],
['2015/11/20', 40, '分支3'], ['2015/11/21', 34, '分支3'], ['2015/11/22', 28, '分支3'],
['2015/11/14', 7, '分支4'], ['2015/11/15', 2, '分支4'], ['2015/11/16', 17, '分支4'],
['2015/11/17', 33, '分支4'], ['2015/11/18', 40, '分支4'], ['2015/11/19', 32, '分支4'],
['2015/11/20', 26, '分支4'], ['2015/11/21', 35, '分支4'], ['2015/11/22', 40, '分支4'],
['2015/11/23', 32, '分支4'], ['2015/11/24', 26, '分支4'], ['2015/11/25', 22, '分支4'],
['2015/11/26', 16, '分支4'], ['2015/11/27', 22, '分支4'], ['2015/11/28', 10, '分支4'],
['2015/11/08', 10, '分支5'], ['2015/11/09', 15, '分支5'], ['2015/11/10', 35, '分支5'],
['2015/11/11', 38, '分支5'], ['2015/11/12', 22, '分支5'], ['2015/11/13', 16, '分支5'],
['2015/11/14', 7, '分支5'], ['2015/11/15', 2, '分支5'], ['2015/11/16', 17, '分支5'],
['2015/11/17', 33, '分支5'], ['2015/11/18', 40, '分支5'], ['2015/11/19', 32, '分支5'],
['2015/11/20', 26, '分支5'], ['2015/11/21', 35, '分支5'], ['2015/11/22', 4, '分支5'],
['2015/11/23', 32, '分支5'], ['2015/11/24', 26, '分支5'], ['2015/11/25', 22, '分支5'],
['2015/11/26', 16, '分支5'], ['2015/11/27', 22, '分支5'], ['2015/11/28', 10, '分支5'],
['2015/11/08', 10, '分支6'], ['2015/11/09', 15, '分支6'], ['2015/11/10', 35, '分支6'],
['2015/11/11', 38, '分支6'], ['2015/11/12', 22, '分支6'], ['2015/11/13', 16, '分支6'],
['2015/11/14', 7, '分支6'], ['2015/11/15', 2, '分支6'], ['2015/11/16', 17, '分支6'],
['2015/11/17', 33, '分支6'], ['2015/11/18', 4, '分支6'], ['2015/11/19', 32, '分支6'],
['2015/11/20', 26, '分支6'], ['2015/11/21', 35, '分支6'], ['2015/11/22', 40, '分支6'],
['2015/11/23', 32, '分支6'], ['2015/11/24', 26, '分支6'], ['2015/11/25', 22, '分支6']
]
#备用颜色列表
colors_list=['#FFA07A','#32CD32','#4169E1','#FAA460','#F0E68C','#8c564b','#e377c2','#7f7f7f','#bcbd22','#17becf']
tr = (
ThemeRiver(init_opts=opts.InitOpts(theme=ThemeType.DARK))
.add(series_name=datax, data=datay, label_opts=opts.LabelOpts(is_show=False, color=colors_list),
singleaxis_opts=opts.SingleAxisOpts(pos_top="50", pos_bottom="50", type_="time"))
.set_global_opts(title_opts=opts.TitleOpts(title='主题河流示例图'),
xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=30)))
)
tr.render_notebook()
关系图
from pyecharts.charts import Graph
import os
import json
with open(os.path.join("data", "weibo-2.json"), "r", encoding="utf-8") as f:
j = json.load(f)
# print(j)
nodes, links, categories, cont, mid, userl = j
# print(mid)
graph = (
Graph(init_opts=opts.InitOpts(theme=ThemeType.WHITE))
.add("", nodes, links, categories, repulsion=50)
.set_series_opts(label_opts=opts.LabelOpts(position='right'),
linestyle_opts=opts.LineStyleOpts(curve=0.2))
.set_global_opts(title_opts=opts.TitleOpts(title='微博转发关系图',
title_textstyle_opts=opts.TextStyleOpts(color='white')),
legend_opts=opts.LegendOpts(is_show=False))
)
graph.render_notebook()