1 PyEcharts-Faker
from pyecharts. faker import Faker
方法属性 说明 对应内容 Faker.clothes [“衬衫”, “毛衣”, “领带”, “裤子”, “风衣”, “高跟鞋”, “袜子”] Faker.values() [106, 111, 145, 33, 20, 138, 141] Faker.drinks [“可乐”, “雪碧”, “橙汁”, “绿茶”, “奶茶”, “百威”, “青岛”] Faker.phones [“小米”, “三星”, “华为”, “苹果”, “魅族”, “VIVO”, “OPPO”] Faker.fruits [“草莓”, “芒果”, “葡萄”, “雪梨”, “西瓜”, “柠檬”, “车厘子”] Faker.animal [“河马”, “蟒蛇”, “老虎”, “大象”, “兔子”, “熊猫”, “狮子”] Faker.cars [“宝马”, “法拉利”, “奔驰”, “奥迪”, “大众”, “丰田”, “特斯拉”] Faker.dogs [“哈士奇”, “萨摩耶”, “泰迪”, “金毛”, “牧羊犬”, “吉娃娃”, “柯基”] Faker.week [“周一”, “周二”, “周三”, “周四”, “周五”, “周六”, “周日”] Faker.week_en [‘Saturday’, ‘Friday’, ‘Thursday’, ‘Wednesday’, ‘Tuesday’, ‘Monday’, ‘Sunday’] Faker.clock [‘12a’,‘1a’,‘2a’,‘3a’,‘4a’,‘5a’,‘6a’,…,4p’,‘5p’,‘6p’,‘7p’,‘8p’,‘9p’,‘10p’,‘11p’] Faker.visual_color [ “#313695”, “#4575b4”, “#74add1”, “#abd9e9”, …] Faker.months ‘1月’, ‘2月’, ‘3月’, ‘4月’, ‘5月’, ‘6月’, ‘7月’, ‘8月’, ‘9月’, ‘10月’, ‘11月’, ‘12月’] Faker.provinces [“广东”, “北京”, “上海”, “江西”, “湖南”, “浙江”, “江苏”] Faker.guangdong_city [“汕头市”, “汕尾市”, “揭阳市”, “阳江市”, “肇庆市”, “广州市”, “惠州市”] Faker.country [‘China’, ‘Canada’, ‘Brazil’, ‘Russia’, ‘United States’, ‘Africa’, ‘Germany’] Faker.days_attrs [‘0天’,‘1天’,‘2天’,‘3天’,‘4天’,‘5天’,‘6天’, ‘7天’,‘8天’,… …,‘28天’,‘29天’] Faker.days_values 生成的从1-30之间的随机天数,顺序是打乱的
2 常用视图
import pyecharts. options as opts
from pyecharts. faker import Faker
from pyecharts. globals import ThemeType
2.1 饼图
from pyecharts. charts import Pie
data_pie = list ( zip ( Faker. choose( ) , Faker. values( ) ) )
pie_img = (
Pie( )
. add( "" , [ list ( x) for x in data_pie] )
. set_colors( [ "red" , "yellow" , "orange" , "pink" , "green" , "blue" , "black" ] )
. set_global_opts(
title_opts= opts. TitleOpts( title= "饮料价格" ) ,
legend_opts= opts. LegendOpts( type_= "scroll" , pos_right= "10px" , orient= "vertaical" )
)
. set_series_opts( label_opts= opts. LabelOpts( formatter= "{b}:{c}" ) )
)
pie_img. render_notebook( )
key_pie = Faker. choose( )
pie_img = (
Pie( )
. add( "" , [ list ( x) for x in zip ( key_pie, Faker. values( ) ) ] ,
radius= [ "30%" , "75%" ] ,
center= [ "25%" , "50%" ] ,
rosetype= "radius"
)
. add( "" , [ list ( x) for x in zip ( key_pie, Faker. values( ) ) ] ,
radius= [ "30%" , "75%" ] ,
center= [ "80%" , "50%" ] ,
rosetype= "radius"
)
)
pie_img. render_notebook( )
2.2 柱形图
from pyecharts. charts import Bar
from pyecharts. charts import Bar
bar_img = (
Bar( )
. add_xaxis( Faker. choose( ) )
. add_yaxis( "淘宝" , Faker. values( ) )
. add_yaxis( "京东" , Faker. values( ) )
. set_global_opts(
xaxis_opts= opts. AxisOpts( axislabel_opts= opts. LabelOpts( rotate= 320 ) )
)
)
bar_img. render_notebook( )
from pyecharts. charts import Bar
bar_img = (
Bar( )
. add_xaxis( Faker. choose( ) )
. add_yaxis( "淘宝" , Faker. values( ) )
. add_yaxis( "京东" , Faker. values( ) )
. reversal_axis( )
. set_series_opts(
label_opts= opts. LabelOpts( position= "right" )
)
)
bar_img. render_notebook( )
bar_img = (
Bar(
init_opts= opts. InitOpts(
animation_opts= opts. AnimationOpts(
animation_delay= 1000 ,
animation_easing= "elastion"
)
)
)
. add_xaxis( Faker. choose( ) )
. add_yaxis( "淘宝" , Faker. values( ) )
. add_yaxis( "京东" , Faker. values( ) )
)
bar_img. render_notebook( )
bar_img = (
Bar( )
. add_xaxis( Faker. choose( ) )
. add_yaxis( "淘宝" , Faker. values( ) , stack= "one" )
. add_yaxis( "京东" , Faker. values( ) , stack= "one" )
)
bar_img. render_notebook( )
2.3 JsCode
from pyecharts. commons. utils import JsCode
bar_img = (
Bar(
init_opts= opts. InitOpts(
bg_color= {
"image" : JsCode( "img" ) ,
"repeat" : "no-repeat"
}
)
)
. add_xaxis( Faker. choose( ) )
. add_yaxis( "淘宝" , Faker. values( ) )
. add_yaxis( "京东" , Faker. values( ) )
)
bar_img. add_js_funcs(
"""
var img = new Image();
img.src = 'https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png'
"""
)
bar_img. render_notebook( )
color_function = """
function(params){
if(params.value>=0 && params.value<=49){
return 'red';
}
else if (params.value>=50 && params.value<=99){
return 'green';
}
else if (params.value>=100 && params.value<=149){
return 'blue';
}
}
"""
bar_img = (
Bar( )
. add_xaxis( Faker. choose( ) )
. add_yaxis( "淘宝" , Faker. values( ) , itemstyle_opts= opts. ItemStyleOpts( color= JsCode( color_function) ) )
. add_yaxis( "京东" , Faker. values( ) , itemstyle_opts= opts. ItemStyleOpts( color= JsCode( color_function) ) )
. add_yaxis( "天猫" , Faker. values( ) , itemstyle_opts= opts. ItemStyleOpts( color= JsCode( color_function) ) )
)
bar_img. render_notebook( )
2.4 象形柱状图
from pyecharts. charts import PictorialBar
from pyecharts. globals import SymbolType
pbar_img = (
PictorialBar( )
. add_xaxis( [ "小明" , "小红" , "小刚" , "王强" , "张伟" , "大伟" , "小帅" ] )
. add_yaxis(
"票数" ,
Faker. values( ) ,
label_opts= opts. LabelOpts( is_show= False ) ,
symbol= SymbolType. ROUND_RECT,
symbol_repeat= "fixed" ,
symbol_size= 20 ,
is_symbol_clip= True
)
. reversal_axis( )
. set_global_opts(
xaxis_opts= opts. AxisOpts( is_show= False ) ,
yaxis_opts= opts. AxisOpts(
axistick_opts= opts. AxisTickOpts( is_show= False ) ,
axisline_opts= opts. AxisLineOpts( is_show= False )
)
)
)
pbar_img. render_notebook( )
2.5 雷达图
from pyecharts. charts import Radar
num1 = [ [ 8000 , 10000 , 7500 , 6800 , 13000 , 15000 ] ]
num2 = [ [ 7000 , 8000 , 9500 , 8800 , 11000 , 12500 ] ]
radar_img = (
Radar( )
. add_schema(
schema= [
opts. RadarIndicatorItem( name= "demo1" , max_= 9000 ) ,
opts. RadarIndicatorItem( name= "demo2" , max_= 11000 ) ,
opts. RadarIndicatorItem( name= "demo3" , max_= 10000 ) ,
opts. RadarIndicatorItem( name= "demo4" , max_= 9000 ) ,
opts. RadarIndicatorItem( name= "demo5" , max_= 14000 ) ,
opts. RadarIndicatorItem( name= "demo6" , max_= 16000 ) ,
]
)
. add( "数据1" , num1, color= "green" )
. add( "数据2" , num2, color= "orange" )
)
radar_img. render_notebook( )
2.6 折线图
from pyecharts. charts import Line
line_img = (
Line( )
. add_xaxis( Faker. choose( ) )
. add_yaxis( "淘宝" , Faker. values( ) )
. add_yaxis( "京东" , Faker. values( ) , is_smooth= True )
. set_global_opts(
tooltip_opts= opts. TooltipOpts( trigger= "axis" )
)
)
line_img. render_notebook( )
line_img = (
Line(
init_opts= opts. InitOpts( width= "1000px" , height= "600px" )
)
. add_xaxis( Faker. week)
. add_yaxis( "淘宝" , Faker. values( ) )
. add_yaxis(
"京东" ,
Faker. values( ) ,
symbol= "triangle" ,
symbol_size= 15 ,
linestyle_opts= opts. LineStyleOpts(
color= "green" , width= 3 , type_= "dashed"
) ,
label_opts= opts. LabelOpts( is_show= False ) ,
itemstyle_opts= opts. ItemStyleOpts(
border_color= "red" , border_width= 2
) ,
markpoint_opts= opts. MarkPointOpts(
data= [
opts. MarkPointItem( type_= "max" ) ,
opts. MarkPointItem( type_= "min" ) ,
]
) ,
markline_opts= opts. MarkLineOpts(
data= [
opts. MarkLineItem( type_= "average" )
]
)
)
. set_global_opts(
tooltip_opts= opts. TooltipOpts( trigger= "axis" )
)
)
line_img. render_notebook( )
line_img = (
Line( )
. add_xaxis( Faker. week)
. add_yaxis(
"淘宝" ,
y_axis= [ 120 , 110 , 140 , 150 , 140 , 140 , 130 ] ,
areastyle_opts= opts. AreaStyleOpts( opacity= 0.7 )
)
. set_global_opts(
tooltip_opts= opts. TooltipOpts( trigger= "axis" ) ,
xaxis_opts= opts. AxisOpts( boundary_gap= False )
)
)
line_img. render_notebook( )
line_img = (
Line( )
. add_xaxis( Faker. week)
. add_yaxis(
"淘宝" ,
stack= "堆叠" ,
y_axis= [ 120 , 110 , 140 , 150 , 140 , 140 , 130 ] ,
areastyle_opts= opts. AreaStyleOpts( opacity= 0.7 ) ,
label_opts= opts. LabelOpts( is_show= False )
)
. add_yaxis(
"天猫" ,
stack= "堆叠" ,
y_axis= [ 120 , 110 , 140 , 150 , 140 , 140 , 130 ] ,
areastyle_opts= opts. AreaStyleOpts( opacity= 0.7 ) ,
label_opts= opts. LabelOpts( is_show= False )
)
. add_yaxis(
"京东" ,
stack= "堆叠" ,
y_axis= [ 120 , 110 , 140 , 150 , 140 , 140 , 130 ] ,
areastyle_opts= opts. AreaStyleOpts( opacity= 0.7 ) ,
label_opts= opts. LabelOpts( is_show= False )
)
. add_yaxis(
"拼多多" ,
stack= "堆叠" ,
y_axis= [ 120 , 110 , 140 , 150 , 140 , 140 , 130 ] ,
areastyle_opts= opts. AreaStyleOpts( opacity= 0.7 ) ,
label_opts= opts. LabelOpts( is_show= False )
)
. set_global_opts(
tooltip_opts= opts. TooltipOpts( trigger= "axis" ) ,
xaxis_opts= opts. AxisOpts( boundary_gap= True )
)
)
line_img. render_notebook( )