Github/GItee仓库地址
注意
数据传入到 pyecharts 的时候,需要自行将数据格式转换成上述 Python 原生的数据格式。 方法:
[ int ( x) for x in your_numpy_array_or_something_else]
[ float ( x) for x in your_numpy_array_or_something_else]
[ str ( x) for x in your_numpy_array_or_something_else]
便捷方法: pandas.Series.tolist()
生成数据
import numpy as np
x_data = [ chr ( i) for i in range ( 65 , 91 ) ]
[ str ( i) for i in x_data]
y_data = np. random. uniform( 5 , 200 , 26 )
[ float ( i) for i in y_data]
data= [ list ( z) for z in zip ( x_data, y_data) ]
乱序数据
南丁格尔图,通过半径区分数据大小,有’radius’和’area’两种模式。
radius:扇区圆心角展现数据的百分比,半径展现数据的大小 area:所有扇区圆心角相同,仅通过半径展现数据大小
from pyecharts. charts import Pie
from pyecharts import options as opts
from pyecharts. globals import ThemeType
fig = Pie( init_opts= opts. InitOpts(
width= '1200px' , height= '600px' , theme= ThemeType. ESSOS, bg_color= 'white' ) )
fig. add(
series_name= "States" ,
data_pair= data,
radius= [ "15%" , "80%" ] ,
center= [ "25%" , "50%" ] ,
rosetype= 'radius' ,
color= 'auto' )
fig. add(
series_name= "States" ,
data_pair= data,
radius= [ "15%" , "80%" ] ,
center= [ "75%" , "50%" ] ,
rosetype= 'area' ,
color= 'auto' )
fig. set_global_opts(
title_opts= opts. TitleOpts( title= "两种南丁格尔图" ) ,
legend_opts= opts. LegendOpts( is_show= False ) ,
visualmap_opts= opts. VisualMapOpts
(
is_show= False ,
type_= 'size' ,
) )
fig. set_series_opts( label_opts= opts. LabelOpts(
is_show= True ,
position= 'left' ,
font_style= 'oblique' ,
font_weight= 'bold' ,
font_family= 'Arial' ,
margin= 15 ,
font_size= 11 )
)
fig. render_notebook( )
渲染图片为png或jpeg
snapshot-selenium 是 pyecharts + selenium 渲染图片的扩展 使用 selenium 需要配置 browser driver,这部分可以参考 selenium-python 相关介绍 推荐使用 Chrome 浏览器,可以开启 headless 模式。目前支持 Chrome, Safari。
from pyecharts. render import make_snapshot
from snapshot_selenium import snapshot
make_snapshot( engine= snapshot, file_name= fig. render( 'rosetype.html' ) , output_name= 'C:\Git Code\ScienceGallery\Picture\Rosetype.png' , is_remove_html= True )
顺序数据
data. sort( key= lambda x: x[ 1 ] )
from pyecharts. charts import Pie
from pyecharts import options as opts
from pyecharts. globals import ThemeType
fig = Pie( init_opts= opts. InitOpts(
width= '1200px' , height= '600px' , theme= ThemeType. ESSOS, bg_color= 'white' ) )
fig. add(
series_name= "States" ,
data_pair= data,
radius= [ "15%" , "80%" ] ,
center= [ "25%" , "50%" ] ,
rosetype= 'radius' ,
is_clockwise= False ,
color= 'auto' )
fig. add(
series_name= "States" ,
data_pair= data,
radius= [ "15%" , "80%" ] ,
center= [ "75%" , "50%" ] ,
rosetype= 'area' ,
is_clockwise= False ,
color= 'auto' )
fig. set_global_opts(
title_opts= opts. TitleOpts( title= "两种南丁格尔图" ) ,
legend_opts= opts. LegendOpts( is_show= False ) ,
visualmap_opts= opts. VisualMapOpts
(
is_show= False ,
type_= 'size' ,
) )
fig. set_series_opts( label_opts= opts. LabelOpts(
is_show= True ,
position= 'left' ,
font_style= 'oblique' ,
font_weight= 'bold' ,
font_family= 'Arial' ,
margin= 15 ,
font_size= 11 )
)
fig. render_notebook( )