Pyecharts_example源码
导入模块
from pyecharts import options as opts
from pyecharts. charts import Bar, Pie, Geo, Map, BMap, Page, WordCloud, Grid, Line, Scatter, Kline
from pyecharts. globals import ThemeType, ChartType, SymbolType, CurrentConfig
from pyecharts. faker import Faker
from pyecharts. commons. utils import JsCode
import os
os. chdir( r"E:\Python\教程\pyecharts-master\pyecharts-master\example" )
os. getcwd( )
'E:\\Python\\教程\\pyecharts-master\\pyecharts-master\\example'
基本图形
Calendar:日历图
Calendar-2017年微信步数情况
import datetime
import random
from pyecharts. charts import Calendar
def calendar_base ( ) - > Calendar:
begin = datetime. date( 2017 , 1 , 1 )
end = datetime. date( 2017 , 12 , 31 )
data = [
[ str ( begin + datetime. timedelta( days= i) ) , random. randint( 1000 , 25000 ) ]
for i in range ( ( end - begin) . days + 1 )
]
c = (
Calendar( )
. add( "" , data, calendar_opts= opts. CalendarOpts( range_= "2017" ) )
. set_global_opts(
title_opts= opts. TitleOpts( title= "Calendar-2017年微信步数情况" ) ,
visualmap_opts= opts. VisualMapOpts(
max_= 20000 ,
min_= 500 ,
orient= "horizontal" ,
is_piecewise= True ,
pos_top= "230px" ,
pos_left= "100px" ,
) ,
)
)
return c
calendar_base( ) . render_notebook( )
<div id="3712fe5fa9b54dd78ae381cfe27eea2b" style="width:900px; height:500px;"></div>
Funnel:漏斗图
Funnel-基本示例
from pyecharts. charts import Funnel, Page
def funnel_base ( ) - > Funnel:
c = (
Funnel( )
. add( "商品" , [ list ( z) for z in zip ( Faker. choose( ) , Faker. values( ) ) ] )
. set_global_opts( title_opts= opts. TitleOpts( title= "Funnel-基本示例" ) )
)
return c
funnel_base( ) . render_notebook( )
<div id="8357aea292724d74b2d1cb79fc5fa181" style="width:900px; height:500px;"></div>
Funnel-Label(inside)
def funnel_label_inside ( ) - > Funnel:
c = (
Funnel( )
. add(
"商品" ,
[ list ( z) for z in zip ( Faker. choose( ) , Faker. values( ) ) ] ,
label_opts= opts. LabelOpts( position= "inside" ) ,
)
. set_global_opts( title_opts= opts. TitleOpts( title= "Funnel-Label(inside)" ) )
)
return c
funnel_label_inside( ) . render_notebook( )
<div id="4603b36bb46a46c7bf22f2cd31cad895" style="width:900px; height:500px;"></div>
Funnel-Sort(ascending)
def funnel_sort_ascending ( ) - > Funnel:
c = (
Funnel( )
. add(
"商品" ,
[ list ( z) for z in zip ( Faker. choose( ) , Faker. values( ) ) ] ,
sort_= "ascending" ,
label_opts= opts. LabelOpts( position= "inside" ) ,
)
. set_global_opts( title_opts= opts. TitleOpts( title= "Funnel-Sort(ascending)" ) )
)
return c
funnel_sort_ascending( ) . render_notebook( )
<div id="1bc78eb05a9c44d9a634ea5b96a5f94b" style="width:900px; height:500px;"></div>
Gauge:仪表盘
Gauge-基本示例
from pyecharts. charts import Gauge, Page
def gauge_base ( ) - > Gauge:
c = (
Gauge( )
. add( "" , [ ( "完成率" , 66.6 ) ] )
. set_global_opts( title_opts= opts. TitleOpts( title= "Gauge-基本示例" ) )
)
return c
gauge_base( ) . render_notebook( )
<div id="2831bc58eff941dc8e6b5e773730793b" style="width:900px; height:500px;"></div>
Gauge-不同颜色
def gauge_color ( ) - > Gauge:
c = (
Gauge( )
. add(
"业务指标" ,
[ ( "完成率" , 55.5 ) ] ,
axisline_opts= opts. AxisLineOpts(
linestyle_opts= opts. LineStyleOpts(
color= [ ( 0.3 , "#67e0e3" ) , ( 0.7 , "#37a2da" ) , ( 1 , "#fd666d" ) ] , width= 30
)
) ,
)
. set_global_opts(
title_opts= opts. TitleOpts( title= "Gauge-不同颜色" ) ,
legend_opts= opts. LegendOpts( is_show= False ) ,
)
)
return c
gauge_color( ) . render_notebook( )
<div id="a07d809b0db44dcfb575ec5dd37a8b12" style="width:900px; height:500px;"></div>
Gauge-分割段数-Label
def gauge_splitnum_label ( ) - > Gauge:
c = (
Gauge( )
. add(
"业务指标" ,
[ ( "完成率" , 55.5 ) ] ,
split_number= 5 ,
axisline_opts= opts. AxisLineOpts(
linestyle_opts= opts. LineStyleOpts(
color= [ ( 0.3 , "#67e0e3" ) , ( 0.7 , "#37a2da" ) , ( 1 , "#fd666d" ) ] , width= 30
)
) ,
)
. set_global_opts(
title_opts= opts. TitleOpts( title= "Gauge-分割段数-Label" ) ,
legend_opts= opts. LegendOpts( is_show= False ) ,
)
)
return c
gauge_splitnum_label( ) . render_notebook( )
<div id="a53ebb9a4077485ba3e699ed3a76ceae" style="width:900px; height:500px;"></div>
Gauge-改变轮盘内的字体
def gauge_label_title_setting ( ) - > Gauge:
c = (
Gauge( )
. add(
"" ,
[ ( "完成率" , 66.6 ) ] ,
title_label_opts= opts. LabelOpts(
font_size= 40 , color= "blue" , font_family= "Microsoft YaHei"
) ,
)
. set_global_opts( title_opts= opts. TitleOpts( title= "Gauge-改变轮盘内的字体" ) )
)
return c
gauge_label_title_setting( ) . render_notebook( )
<div id="125298ae062447a58414cbbb669fcfad" style="width:900px; height:500px;"></div>
Graph:关系图
Graph-基本示例
import json
import os
from pyecharts. charts import Graph, Page
def graph_base ( ) - > Graph:
nodes = [
{ "name" : "结点1" , "symbolSize" : 10 } ,
{ "name" : "结点2" , "symbolSize" : 20 } ,
{ "name" : "结点3" , "symbolSize" : 30 } ,
{ "name" : "结点4" , "symbolSize" : 40 } ,
{ "name" : "结点5" , "symbolSize" : 50 } ,
{ "name" : "结点6" , "symbolSize" : 40 } ,
{ "name" : "结点7" , "symbolSize" : 30 } ,
{ "name" : "结点8" , "symbolSize" : 20 } ,
]
links = [ ]
for i in nodes:
for j in nodes:
links. append( { "source" : i. get( "name" ) , "target" : j. get( "name" ) } )
c = (
Graph( )
. add( "" , nodes, links, repulsion= 8000 )
. set_global_opts( title_opts= opts. TitleOpts( title= "Graph-基本示例" ) )
)
return c
graph_base( ) . render_notebook( )
<div id="d71a48116e9940c889ed0e3cb688e09f" style="width:900px; height:500px;"></div>
Graph-GraphNode-GraphLink
def graph_with_opts ( ) - > Graph:
nodes = [
opts. GraphNode( name= "结点1" , symbol_size= 10 ) ,
opts. GraphNode( name= "结点2" , symbol_size= 20 ) ,
opts. GraphNode( name= "结点3" , symbol_size= 30 ) ,
opts. GraphNode( name= "结点4" , symbol_size= 40 ) ,
opts. GraphNode( name= "结点5" , symbol_size= 50 ) ,
]
links = [
opts. GraphLink( source= "结点1" , target= "结点2" ) ,
opts. GraphLink( source= "结点2" , target= "结点3" ) ,
opts. GraphLink( source= "结点3" , target= "结点4" ) ,
opts. GraphLink( source= "结点4" , target= "结点5" ) ,
opts. GraphLink( source= "结点5" , target= "结点1" ) ,
]
c = (
Graph( )
. add( "" , nodes, links, repulsion= 4000 )
. set_global_opts( title_opts= opts. TitleOpts( title= "Graph-GraphNode-GraphLink" ) )
)
return c
graph_with_opts( ) . render_notebook( )
<div id="df812eb5762f42cd97887603469cc042" style="width:900px; height:500px;"></div>
Graph-GraphNode-GraphLink-WithEdgeLabel
def graph_with_edge_opts ( ) - > Graph:
nodes_data = [
opts. GraphNode( name= "结点1" , symbol_size= 10 ) ,
opts. GraphNode( name= "结点2" , symbol_size= 20 ) ,
opts. GraphNode( name= "结点3" , symbol_size= 30 ) ,
opts. GraphNode( name= "结点4" , symbol_size= 40 ) ,
opts. GraphNode( name= "结点5" , symbol_size= 50 ) ,
opts. GraphNode( name= "结点6" , symbol_size= 60 ) ,
]
links_data = [
opts. GraphLink( source= "结点1" , target= "结点2" , value= 2 ) ,
opts. GraphLink( source= "结点2" , target= "结点3" , value= 3 ) ,
opts. GraphLink( source= "结点3" , target= "结点4" , value= 4 ) ,
opts. GraphLink( source= "结点4" , target= "结点5" , value= 5 ) ,
opts. GraphLink( source= "结点5" , target= "结点6" , value= 6 ) ,
opts. GraphLink( source= "结点6" , target= "结点1" , value= 7 ) ,
]
c = (
Graph( )
. add(
"" ,
nodes_data,
links_data,
repulsion= 4000 ,
edge_label= opts. LabelOpts(
is_show= True ,
position= "middle" ,
formatter= "{b} 的数据 {c}" ,
) ,
)
. set_global_opts(
title_opts= opts. TitleOpts( title= "Graph-GraphNode-GraphLink-WithEdgeLabel" )
)
)
return c
graph_with_edge_opts( ) . render_notebook( )
<div id="67f0b0da310145519880384a07f86221" style="width:900px; height:500px;"></div>
Graph-微博转发关系图
def graph_weibo ( ) - > Graph:
with open ( os. path. join( "fixtures" , "weibo.json" ) , "r" , encoding= "utf-8" ) as f:
j = json. load( f)
nodes, links, categories, cont, mid, userl = j
c = (
Graph( )
. add(
"" ,
nodes,
links,
categories,
repulsion= 50 ,
linestyle_opts= opts. LineStyleOpts( curve= 0.2 ) ,
label_opts= opts. LabelOpts( is_show= False ) ,
)
. set_global_opts(
legend_opts= opts. LegendOpts( is_show= False ) ,
title_opts= opts. TitleOpts( title= "Graph-微博转发关系图" ) ,
)
)
return c
graph_weibo( ) . render_notebook( )
<div id="5b2d56203c6b433a96b422e5703f7ceb" style="width:900px; height:500px;"></div>