整理自官网
from pyecharts.globals import CurrentConfig, OnlineHostType CurrentConfig.ONLINE_HOST = OnlineHostType.NOTEBOOK_HOST
from pyecharts.charts import Scatter,EffectScatter import numpy as np y = np.random.randint(1,30,50).tolist()
s = EffectScatter() s.add_xaxis(xaxis_data=range(50)) s.add_yaxis('A', y_axis=y, symbol='pin',symbol_size=16,label_opts=opts.LabelOpts(is_show=False)) s.set_global_opts( title_opts=opts.TitleOpts(title='散点图-涟漪特效散点'), xaxis_opts=opts.AxisOpts(name='秒'), yaxis_opts=opts.AxisOpts(name='流量'), legend_opts=opts.LegendOpts(pos_right=50), # series_index指定映射的系列索引 visualmap_opts=[ opts.VisualMapOpts(type_='color', min_=0,max_=50,series_index=0, pos_top='20px'), opts.VisualMapOpts(type_='size', min_=0,max_=50,series_index=1, pos_top='60%') ] ) s2 = Scatter() s2.add_xaxis(xaxis_data=range(50)) s2.add_yaxis('B', y_axis=y, xaxis_index=1, yaxis_index=1, label_opts=opts.LabelOpts(is_show=False)) s2.set_global_opts( xaxis_opts=opts.AxisOpts(name='秒', grid_index=1), yaxis_opts=opts.AxisOpts(name='流量'), legend_opts=opts.LegendOpts(pos_right=10), ) g = Grid(init_opts=opts.InitOpts(height='450px', width='850px')) g.add(s,grid_opts=opts.GridOpts(height="35%")) # 高度变为35% g.add(s2,grid_opts=opts.GridOpts(pos_top='60%',height="35%")) # 距离容器顶部的距离 g.render_notebook()
from pyecharts.charts import Parallel
r = np.random.rand(12) data = [[i, round(j,2), round(m,2), n] for i,j,m,n in zip(range(12),r,np.random.randint(1,3,12)+r, np.random.choice(['A','B'],12))] data[:3]
[[0, 0.04, 1.04, ‘A’], [1, 0.9, 1.9, ‘A’], [2, 0.11, 2.11, ‘B’]]
# 坐标轴设置,类别轴设置data所有的类别 schema = [ {'dim':0,'name':'月份'},{'dim':1,'name':'指标1'}, {'dim':2,'name':'指标2'},{'dim':3,'name':'类别','type':'category', 'data':['A','B']}, ] p =Parallel(init_opts=opts.InitOpts(height='350px', width='850px')) p.add_schema(schema=schema) p.add('2015', data, linestyle_opts=opts.LineStyleOpts(color='#CD0000')) # 没有color参数 r = np.random.rand(12) data = [[i, round(j,2), round(m,2), n] for i,j,m,n in zip(range(12),r,np.random.randint(1,3,12)+r, np.random.choice(['A','B'],12))] p.add('2016', data, linestyle_opts=opts.LineStyleOpts(color='#5CACEE')) p.render_notebook()
p =Parallel(init_opts=opts.InitOpts(height='350px', width='850px')) # 单独设置每个轴,刻度范围 p.add_schema( schema=[ opts.ParallelAxisOpts(dim=0, name='月份'), opts.ParallelAxisOpts(dim=1, name='指标1',min_=0,max_=1), opts.ParallelAxisOpts(dim=2, name='指标2',min_=1,max_=3), opts.ParallelAxisOpts(dim=3, name='类别',type_='category', data=['A','B']), ] ) p.add('2015', data, is_smooth=True) r = np.random.rand(12) data = [[i, round(j,2), round(m,2), n] for i,j,m,n in zip(range(12),r,np.random.randint(1,3,12)+r, np.random.choice(['A','B'],12))] p.add('2016', data, is_smooth=True) p.render_notebook()
from pyecharts.charts import Sankey from pyecharts import options as opts import numpy as np nodes1 = [{'name':'a%s'%i} for i in range(3)] nodes2 = [{'name':'b%s'%i} for i in range(3)] nodes3 = [{'name':'c%s'%i} for i in range(3)] nodes = nodes1+nodes2+nodes3 nodes
[{‘name’: ‘a0’},
{‘name’: ‘a1’},
{‘name’: ‘a2’},
{‘name’: ‘b0’},
{‘name’: ‘b1’},
{‘name’: ‘b2’},
{‘name’: ‘c0’},
{‘name’: ‘c1’},
{‘name’: ‘c2’}]
links1 = [{'source':i['name'],'target':j['name'],'value':int(np.random.randint(1,10))} for i in nodes1 for j in nodes2] links2 = [{'source':i['name'],'target':j['name'],'value':int(np.random.randint(1,10))} for i in nodes2 for j in nodes3] links = links1+links2 links[:5]
[{‘source’: ‘a0’, ‘target’: ‘b0’, ‘value’: 8},
{‘source’: ‘a0’, ‘target’: ‘b1’, ‘value’: 9},
{‘source’: ‘a0’, ‘target’: ‘b2’, ‘value’: 3},
{‘source’: ‘a1’, ‘target’: ‘b0’, ‘value’: 2},
{‘source’: ‘a1’, ‘target’: ‘b1’, ‘value’: 6}]
s = Sankey(init_opts=opts.InitOpts(height='350px', width='650px')) s.add( "sankey", nodes=nodes, links=links, linestyle_opt=opts.LineStyleOpts(opacity=0.2, curve=0.5, color="source"), label_opts=opts.LabelOpts(position="right"), # 选择方向,默认 orient='horizontal', # 设置每一层的颜色,线型 levels=[opts.SankeyLevelsOpts( depth=0, itemstyle_opts=opts.ItemStyleOpts(color='#006699'), linestyle_opts=opts.LineStyleOpts(color="source", opacity=0.6), ),opts.SankeyLevelsOpts( depth=1, itemstyle_opts=opts.ItemStyleOpts(color='#009966'), linestyle_opts=opts.LineStyleOpts(color="source", opacity=0.6), ), opts.SankeyLevelsOpts( depth=2, itemstyle_opts=opts.ItemStyleOpts(color='#FF9933'), linestyle_opts=opts.LineStyleOpts(color="source", opacity=0.6), ), ], ) s .set_global_opts(title_opts=opts.TitleOpts(title="Sankey-基本示例")) s.render_notebook()
from pyecharts.charts import Graph cate = [{'name':i} for i in ['A','B','C']] values=np.random.randint(10,20,20).tolist() nodes_data = [ {"name": "结点%s"%i, "symbolSize":value*2,'value': value,'category':np.random.randint(0,3)} for i,value in zip(range(1,21),values) ] links_data = [ {"source": i.get("name"), "target": j.get("name"), 'value':np.random.randint(1,10)} for i in nodes_data[0:20:2] for j in nodes_data[1:20:2] ] # 也可以这样设置: # nodes_data = [ # opts.GraphNode(name="结点1", symbol_size=10,value=20), # 节点项:节点名称,节点大小,节点的值 # opts.GraphNode(name="结点2", symbol_size=20,value=15), # ] # links_data = [ # opts.GraphLink(source="结点1", target="结点2", value=2), # 关系项:主节点,对应的节点,值 # opts.GraphLink(source="结点1", target="结点4", value=20), # opts.GraphLink(source="结点2", target="结点3", value=3), # ]
nodes_data[:5]
[{‘category’: 1, ‘name’: ‘结点1’, ‘symbolSize’: 34, ‘value’: 17},
{‘category’: 2, ‘name’: ‘结点2’, ‘symbolSize’: 38, ‘value’: 19},
{‘category’: 2, ‘name’: ‘结点3’, ‘symbolSize’: 30, ‘value’: 15},
{‘category’: 2, ‘name’: ‘结点4’, ‘symbolSize’: 28, ‘value’: 14},
{‘category’: 2, ‘name’: ‘结点5’, ‘symbolSize’: 28, ‘value’: 14}]
links_data[:5]
[{‘source’: ‘结点1’, ‘target’: ‘结点2’, ‘value’: 7},
{‘source’: ‘结点1’, ‘target’: ‘结点4’, ‘value’: 2},
{‘source’: ‘结点1’, ‘target’: ‘结点6’, ‘value’: 8},
{‘source’: ‘结点1’, ‘target’: ‘结点8’, ‘value’: 7},
{‘source’: ‘结点1’, ‘target’: ‘结点10’, ‘value’: 8}]
g = Graph(init_opts=opts.InitOpts(height='450px', width='650px')) g.add(series_name="", # 项目名称 nodes=nodes_data, # 节点数据 links=links_data, # 节点关系数据 categories=cate, is_rotate_label=True, # 标签旋转 repulsion=4000, # 斥力值,值越大斥力越大,节点之间越远(最后的图也就越大) # gravity=0.8,# 引力因子,和上面相反 layout='circular', # 图的布局。可选:'none' 不采用任何布局,使用节点中提供的 x, y 作为节点的位置。 'circular' 环形布局。'force' 力引导布局。 # layout:采用关系力引导布局方式,节点之间的值越大,节点之间也就越近 edge_length=[1,50], # 线的长度,值范围越大,关系亲疏越明显(近的更近) edge_label=opts.LabelOpts(is_show=False, position="middle"), # 节点之间的边(线)的标签设置 itemstyle_opts=opts.ItemStyleOpts(border_color='black', opacity='0.9'), # 节点样式 # label_opts=opts.LabelOpts(is_show=True, position='outside', color='black') # 节点上的标签的设置 ) g.set_global_opts(title_opts=opts.TitleOpts(title="关系图示例"), legend_opts=opts.LegendOpts(pos_right='40px') # visualmap_opts=opts.VisualMapOpts(max_=20) # 颜色映射根据节点的值,即nodes中的value ) g.render_notebook()
from pyecharts.charts import Sunburst colors = ["#CD0000",'#009966','#FF9900'] #第1层颜色: colors2 = [['#FF6699','#FF9999','#CC3366'],['#00CC33','#66CC00','#339900'],['#FFCC66','#FF9933']]# 第2层颜色 names = [['c1','c2','c3'],['c4','c5','c6'],['c7','c8']] # 第二层节点名称 values = [[10,15,29],[34,17,26],[20,16]] # 二层节点数值,一层节点可以没有数值,此时为和 level2 = [ [{'name':name[i], 'itemStyle':{'color':color[j]},'value':value[n]} for i,j,n in zip(range(len(name)),range(len(color)),range(len(value)))] for name,color,value in zip(names, colors2,values) ] data = [{'name':i, 'itemStyle':{'color':j}, 'children': m} for i,j,m in zip(['Fruits','Vegetables','Others'],colors,level2)] data
[{‘children’: [{‘itemStyle’: {‘color’: ‘#FF6699’}, ‘name’: ‘c1’, ‘value’: 10},
{‘itemStyle’: {‘color’: ‘#FF9999’}, ‘name’: ‘c2’, ‘value’: 15},
{‘itemStyle’: {‘color’: ‘#CC3366’}, ‘name’: ‘c3’, ‘value’: 29}],
‘itemStyle’: {‘color’: ‘#CD0000’},
‘name’: ‘Fruits’},
{‘children’: [{‘itemStyle’: {‘color’: ‘#00CC33’}, ‘name’: ‘c4’, ‘value’: 34},
{‘itemStyle’: {‘color’: ‘#66CC00’}, ‘name’: ‘c5’, ‘value’: 17},
{‘itemStyle’: {‘color’: ‘#339900’}, ‘name’: ‘c6’, ‘value’: 26}],
‘itemStyle’: {‘color’: ‘#009966’},
‘name’: ‘Vegetables’},
{‘children’: [{‘itemStyle’: {‘color’: ‘#FFCC66’}, ‘name’: ‘c7’, ‘value’: 20},
{‘itemStyle’: {‘color’: ‘#FF9933’}, ‘name’: ‘c8’, ‘value’: 16}],
‘itemStyle’: {‘color’: ‘#FF9900’},
‘name’: ‘Others’}]
sun = Sunburst(init_opts=opts.InitOpts(width="850px", height="350px")) sun.add('',data_pair=data, radius=['20%','90%'], highlight_policy='descendant', # 指向后高亮其子节点 sort_='desc', # 排序方式 # 层级配置:可选 levels=[ {}, # 里面的空白 {'label':{'rotate':'tangential'}}, # 第一层:标签旋转 {'label':{'position':'outside','padding':3}, "itemStyle": {"borderWidth": 3} } # 第二层 ] ) sun.set_global_opts(title_opts=opts.TitleOpts(title="Sunburst")) sun.render_notebook()
from pyecharts.charts import TreeMap values = [[10,15,29],[34,17,26],[20,16]] # 二层节点数值 names = [['c1','c2','c3'],['c4','c5','c6'],['c7','c8']] # 第二层节点名称 childs = [ [{'name':name[i], 'value':value[j]} for i,j in zip(range(len(name)),range(len(value)))] for name,value in zip(names, values) ] data = [{'name':i, 'value':j, 'children': m} for i,j,m in zip(['Fruits','Vegetables','Others'],[54,77,36],childs)] data # [{'value':10,'name':'节点1', # 'children':[{'value':3,'name':'节点1.子节点1'}, # {'value':7,'name':'节点1.子节点2'} # ] # }, # { }, # ]
[{‘children’: [{‘name’: ‘c1’, ‘value’: 10},
{‘name’: ‘c2’, ‘value’: 15},
{‘name’: ‘c3’, ‘value’: 29}],
‘name’: ‘Fruits’,
‘value’: 54},
{‘children’: [{‘name’: ‘c4’, ‘value’: 34},
{‘name’: ‘c5’, ‘value’: 17},
{‘name’: ‘c6’, ‘value’: 26}],
‘name’: ‘Vegetables’,
‘value’: 77},
{‘children’: [{‘name’: ‘c7’, ‘value’: 20}, {‘name’: ‘c8’, ‘value’: 16}],
‘name’: ‘Others’,
‘value’: 36}]
treemap = TreeMap(init_opts=opts.InitOpts(height='450px',width='850px')) treemap.add(series_name='示例',data=data,leaf_depth=2, # 设置每一层的 # levels=[ # opts.TreeMapLevelsOpts(color_mapping_by='value'), # opts.TreeMapLevelsOpts(color_mapping_by='value'), # ] ) treemap.set_global_opts(title_opts=opts.TitleOpts(title='矩形树图')) treemap.render_notebook()
联系客服