?87.Grid:并行多图
链接
class pyecharts.charts.Grid(Base)
class Grid (
init_opts: opts. InitOpts = opts. InitOpts( )
)
func pyecharts.charts.Grid.add
def add (
chart: Chart,
grid_opts: Union[ opts. GridOpts, dict ] ,
grid_index: int = 0 ,
is_control_axis_index: bool = False ,
)
GridOpts:直角坐标系网格配置项 class pyecharts.options.GridOpts
class GridOpts (
is_show: bool = False ,
z_level: Numeric = 0 ,
z: Numeric = 2 ,
pos_left: Union[ Numeric, str , None ] = None ,
pos_top: Union[ Numeric, str , None ] = None ,
pos_right: Union[ Numeric, str , None ] = None ,
pos_bottom: Union[ Numeric, str , None ] = None ,
width: Union[ Numeric, str , None ] = None ,
height: Union[ Numeric, str , None ] = None ,
is_contain_label: bool = False ,
background_color: str = "transparent" ,
border_color: str = "#ccc" ,
border_width: Numeric = 1 ,
tooltip_opts: Union[ TooltipOpts, dict , None ] = None ,
)
?87.1 Grid - Grid_overlap_multi_xy_axis
链接
from pyecharts import options as opts
from pyecharts. charts import Bar, Grid, Line
bar = (
Bar( )
. add_xaxis( [ "{}月" . format ( i) for i in range ( 1 , 13 ) ] )
. add_yaxis(
"蒸发量" ,
[ 2.0 , 4.9 , 7.0 , 23.2 , 25.6 , 76.7 , 135.6 , 162.2 , 32.6 , 20.0 , 6.4 , 3.3 ] ,
yaxis_index= 0 ,
color= "#d14a61" ,
)
. add_yaxis(
"降水量" ,
[ 2.6 , 5.9 , 9.0 , 26.4 , 28.7 , 70.7 , 175.6 , 182.2 , 48.7 , 18.8 , 6.0 , 2.3 ] ,
yaxis_index= 1 ,
color= "#5793f3" ,
)
. extend_axis(
yaxis= opts. AxisOpts(
name= "蒸发量" ,
type_= "value" ,
min_= 0 ,
max_= 250 ,
position= "right" ,
axisline_opts= opts. AxisLineOpts(
linestyle_opts= opts. LineStyleOpts( color= "#d14a61" )
) ,
axislabel_opts= opts. LabelOpts( formatter= "{value} ml" ) ,
)
)
. extend_axis(
yaxis= opts. AxisOpts(
type_= "value" ,
name= "温度" ,
min_= 0 ,
max_= 25 ,
position= "left" ,
axisline_opts= opts. AxisLineOpts(
linestyle_opts= opts. LineStyleOpts( color= "#675bba" )
) ,
axislabel_opts= opts. LabelOpts( formatter= "{value} °C" ) ,
splitline_opts= opts. SplitLineOpts(
is_show= True , linestyle_opts= opts. LineStyleOpts( opacity= 1 )
) ,
)
)
. set_global_opts(
yaxis_opts= opts. AxisOpts(
name= "降水量" ,
min_= 0 ,
max_= 250 ,
position= "right" ,
offset= 80 ,
axisline_opts= opts. AxisLineOpts(
linestyle_opts= opts. LineStyleOpts( color= "#5793f3" )
) ,
axislabel_opts= opts. LabelOpts( formatter= "{value} ml" ) ,
) ,
title_opts= opts. TitleOpts( title= "Grid-Overlap-多 X/Y 轴示例" ) ,
tooltip_opts= opts. TooltipOpts( trigger= "axis" , axis_pointer_type= "cross" ) ,
legend_opts= opts. LegendOpts( pos_left= "25%" ) ,
)
)
line = (
Line( )
. add_xaxis( [ "{}月" . format ( i) for i in range ( 1 , 13 ) ] )
. add_yaxis(
"平均温度" ,
[ 2.0 , 2.2 , 3.3 , 4.5 , 6.3 , 10.2 , 20.3 , 23.4 , 23.0 , 16.5 , 12.0 , 6.2 ] ,
yaxis_index= 2 ,
color= "#675bba" ,
label_opts= opts. LabelOpts( is_show= False ) ,
)
)
bar1 = (
Bar( )
. add_xaxis( [ "{}月" . format ( i) for i in range ( 1 , 13 ) ] )
. add_yaxis(
"蒸发量 1" ,
[ 2.0 , 4.9 , 7.0 , 23.2 , 25.6 , 76.7 , 135.6 , 162.2 , 32.6 , 20.0 , 6.4 , 3.3 ] ,
color= "#d14a61" ,
xaxis_index= 1 ,
yaxis_index= 3 ,
)
. add_yaxis(
"降水量 2" ,
[ 2.6 , 5.9 , 9.0 , 26.4 , 28.7 , 70.7 , 175.6 , 182.2 , 48.7 , 18.8 , 6.0 , 2.3 ] ,
color= "#5793f3" ,
xaxis_index= 1 ,
yaxis_index= 3 ,
)
. extend_axis(
yaxis= opts. AxisOpts(
name= "蒸发量" ,
type_= "value" ,
min_= 0 ,
max_= 250 ,
position= "right" ,
axisline_opts= opts. AxisLineOpts(
linestyle_opts= opts. LineStyleOpts( color= "#d14a61" )
) ,
axislabel_opts= opts. LabelOpts( formatter= "{value} ml" ) ,
)
)
. extend_axis(
yaxis= opts. AxisOpts(
type_= "value" ,
name= "温度" ,
min_= 0 ,
max_= 25 ,
position= "left" ,
axisline_opts= opts. AxisLineOpts(
linestyle_opts= opts. LineStyleOpts( color= "#675bba" )
) ,
axislabel_opts= opts. LabelOpts( formatter= "{value} °C" ) ,
splitline_opts= opts. SplitLineOpts(
is_show= True , linestyle_opts= opts. LineStyleOpts( opacity= 1 )
) ,
)
)
. set_global_opts(
xaxis_opts= opts. AxisOpts( grid_index= 1 ) ,
yaxis_opts= opts. AxisOpts(
name= "降水量" ,
min_= 0 ,
max_= 250 ,
position= "right" ,
offset= 80 ,
grid_index= 1 ,
axisline_opts= opts. AxisLineOpts(
linestyle_opts= opts. LineStyleOpts( color= "#5793f3" )
) ,
axislabel_opts= opts. LabelOpts( formatter= "{value} ml" ) ,
) ,
tooltip_opts= opts. TooltipOpts( trigger= "axis" , axis_pointer_type= "cross" ) ,
legend_opts= opts. LegendOpts( pos_left= "65%" ) ,
)
)
line1 = (
Line( )
. add_xaxis( [ "{}月" . format ( i) for i in range ( 1 , 13 ) ] )
. add_yaxis(
"平均温度 1" ,
[ 2.0 , 2.2 , 3.3 , 4.5 , 6.3 , 10.2 , 20.3 , 23.4 , 23.0 , 16.5 , 12.0 , 6.2 ] ,
color= "#675bba" ,
label_opts= opts. LabelOpts( is_show= False ) ,
xaxis_index= 1 ,
yaxis_index= 5 ,
)
)
overlap_1 = bar. overlap( line)
overlap_2 = bar1. overlap( line1)
grid = (
Grid( init_opts= opts. InitOpts( width= "1200px" , height= "800px" ) )
. add(
overlap_1, grid_opts= opts. GridOpts( pos_right= "58%" ) , is_control_axis_index= True
)
. add( overlap_2, grid_opts= opts. GridOpts( pos_left= "58%" ) , is_control_axis_index= True )
. render( "grid_overlap_multi_xy_axis.html" )
)
?87.2 Grid - Grid_multi_yaxis
链接
from pyecharts import options as opts
from pyecharts. charts import Bar, Grid, Line
x_data = [ "{}月" . format ( i) for i in range ( 1 , 13 ) ]
bar = (
Bar( )
. add_xaxis( x_data)
. add_yaxis(
"蒸发量" ,
[ 2.0 , 4.9 , 7.0 , 23.2 , 25.6 , 76.7 , 135.6 , 162.2 , 32.6 , 20.0 , 6.4 , 3.3 ] ,
yaxis_index= 0 ,
color= "#d14a61" ,
)
. add_yaxis(
"降水量" ,
[ 2.6 , 5.9 , 9.0 , 26.4 , 28.7 , 70.7 , 175.6 , 182.2 , 48.7 , 18.8 , 6.0 , 2.3 ] ,
yaxis_index= 1 ,
color= "#5793f3" ,
)
. extend_axis(
yaxis= opts. AxisOpts(
name= "蒸发量" ,
type_= "value" ,
min_= 0 ,
max_= 250 ,
position= "right" ,
axisline_opts= opts. AxisLineOpts(
linestyle_opts= opts. LineStyleOpts( color= "#d14a61" )
) ,
axislabel_opts= opts. LabelOpts( formatter= "{value} ml" ) ,
)
)
. extend_axis(
yaxis= opts. AxisOpts(
type_= "value" ,
name= "温度" ,
min_= 0 ,
max_= 25 ,
position= "left" ,
axisline_opts= opts. AxisLineOpts(
linestyle_opts= opts. LineStyleOpts( color= "#675bba" )
) ,
axislabel_opts= opts. LabelOpts( formatter= "{value} °C" ) ,
splitline_opts= opts. SplitLineOpts(
is_show= True , linestyle_opts= opts. LineStyleOpts( opacity= 1 )
) ,
)
)
. set_global_opts(
yaxis_opts= opts. AxisOpts(
name= "降水量" ,
min_= 0 ,
max_= 250 ,
position= "right" ,
offset= 80 ,
axisline_opts= opts. AxisLineOpts(
linestyle_opts= opts. LineStyleOpts( color= "#5793f3" )
) ,
axislabel_opts= opts. LabelOpts( formatter= "{value} ml" ) ,
) ,
title_opts= opts. TitleOpts( title= "Grid-多 Y 轴示例" ) ,
tooltip_opts= opts. TooltipOpts( trigger= "axis" , axis_pointer_type= "cross" ) ,
)
)
line = (
Line( )
. add_xaxis( x_data)
. add_yaxis(
"平均温度" ,
[ 2.0 , 2.2 , 3.3 , 4.5 , 6.3 , 10.2 , 20.3 , 23.4 , 23.0 , 16.5 , 12.0 , 6.2 ] ,
yaxis_index= 2 ,
color= "#675bba" ,
label_opts= opts. LabelOpts( is_show= False ) ,
)
)
bar. overlap( line)
grid = Grid( )
grid. add( bar, opts. GridOpts( pos_left= "5%" , pos_right= "20%" ) , is_control_axis_index= True )
grid. render( "grid_multi_yaxis.html" )
87.3 Grid - Grid_vertical
链接
from pyecharts import options as opts
from pyecharts. charts import Bar, Grid, Line
from pyecharts. faker import Faker
bar = (
Bar( )
. add_xaxis( Faker. choose( ) )
. add_yaxis( "商家A" , Faker. values( ) )
. add_yaxis( "商家B" , Faker. values( ) )
. set_global_opts( title_opts= opts. TitleOpts( title= "Grid-Bar" ) )
)
line = (
Line( )
. add_xaxis( Faker. choose( ) )
. add_yaxis( "商家A" , Faker. values( ) )
. add_yaxis( "商家B" , Faker. values( ) )
. set_global_opts(
title_opts= opts. TitleOpts( title= "Grid-Line" , pos_top= "48%" ) ,
legend_opts= opts. LegendOpts( pos_top= "48%" ) ,
)
)
grid = (
Grid( )
. add( bar, grid_opts= opts. GridOpts( pos_bottom= "60%" ) )
. add( line, grid_opts= opts. GridOpts( pos_top= "60%" ) )
. render( "grid_vertical.html" )
)
87.4 Grid - Grid_geo_bar
链接
from pyecharts import options as opts
from pyecharts. charts import Bar, Geo, Grid
from pyecharts. faker import Faker
bar = (
Bar( )
. add_xaxis( Faker. choose( ) )
. add_yaxis( "商家A" , Faker. values( ) )
. add_yaxis( "商家B" , Faker. values( ) )
. set_global_opts( legend_opts= opts. LegendOpts( pos_left= "20%" ) )
)
geo = (
Geo( )
. add_schema( maptype= "china" )
. add( "geo" , [ list ( z) for z in zip ( Faker. provinces, Faker. values( ) ) ] )
. set_series_opts( label_opts= opts. LabelOpts( is_show= False ) )
. set_global_opts(
visualmap_opts= opts. VisualMapOpts( ) ,
title_opts= opts. TitleOpts( title= "Grid-Geo-Bar" ) ,
)
)
grid = (
Grid( )
. add( bar, grid_opts= opts. GridOpts( pos_top= "50%" , pos_right= "75%" ) )
. add( geo, grid_opts= opts. GridOpts( pos_left= "60%" ) )
. render( "grid_geo_bar.html" )
)
87.5 Grid - Grid_horizontal
链接
from pyecharts import options as opts
from pyecharts. charts import Grid, Line, Scatter
from pyecharts. faker import Faker
scatter = (
Scatter( )
. add_xaxis( Faker. choose( ) )
. add_yaxis( "商家A" , Faker. values( ) )
. add_yaxis( "商家B" , Faker. values( ) )
. set_global_opts(
title_opts= opts. TitleOpts( title= "Grid-Scatter" ) ,
legend_opts= opts. LegendOpts( pos_left= "20%" ) ,
)
)
line = (
Line( )
. add_xaxis( Faker. choose( ) )
. add_yaxis( "商家A" , Faker. values( ) )
. add_yaxis( "商家B" , Faker. values( ) )
. set_global_opts(
title_opts= opts. TitleOpts( title= "Grid-Line" , pos_right= "5%" ) ,
legend_opts= opts. LegendOpts( pos_right= "20%" ) ,
)
)
grid = (
Grid( )
. add( scatter, grid_opts= opts. GridOpts( pos_left= "55%" ) )
. add( line, grid_opts= opts. GridOpts( pos_right= "55%" ) )
. render( "grid_horizontal.html" )
)
88.Page:顺序多图
链接
class pyecharts.charts.Page
class Page (
page_title: str = "Awesome-pyecharts" ,
js_host: str = "" ,
interval: int = 1 ,
layout: Union[ PageLayoutOpts, dict ] = PageLayoutOpts( )
)
func pyecharts.charts.Page.add
def add ( * charts)
class pyecharts.options.PageLayoutOpts
class PageLayoutOpts (
justify_content: Optional[ str ] = None ,
margin: Optional[ str ] = None ,
display: Optional[ str ] = None ,
flex_wrap: Optional[ str ] = None ,
)
func pyecharts.charts.Page.save_resize_html 用于 DraggablePageLayout 布局重新渲染图表
def save_resize_html (
source: str = "render.html" ,
* ,
cfg_file: types. Optional[ str ] = None ,
cfg_dict: types. Optional[ list ] = None ,
dest: str = "resize_render.html" ,
) - > str
Page 内置了以下布局 SimplePageLayout DraggablePageLayout
88.1 Page Demo
链接
page = Page( )
page. add( bar_datazoom_slider( ) , line_markpoint( ) , pie_rosetype( ) , grid_mutil_yaxis( ) )
page. render( )
page = Page( layout= Page. SimplePageLayout)
page. add( bar_datazoom_slider( ) , line_markpoint( ) , pie_rosetype( ) , grid_mutil_yaxis( ) )
page. render( )
page = Page( layout= Page. DraggablePageLayout)
page. add( bar_datazoom_slider( ) , line_markpoint( ) , pie_rosetype( ) , grid_mutil_yaxis( ) )
page. render( )
Note: DraggablePageLayout 需要 pyecharts 版本 v.1.4.0+
page = Page( layout= Page. DraggablePageLayout)
page. add( bar_datazoom_slider( ) , line_markpoint( ) , pie_rosetype( ) , grid_mutil_yaxis( ) )
page. render( )
Page. save_resize_html( "render.html" , cfg_file= "~/chart_config.json" , dest= "my_new_charts.html" )
Page. save_resize_html( "render.html" , cfg_dict= cfg_dict, dest= "my_new_charts.html" )
page. add( bar_datazoom_slider( ) , line_markpoint( ) , pie_rosetype( ) ) )
88.2 Page - Page_draggable_layout
链接
from pyecharts import options as opts
from pyecharts. charts import Bar, Grid, Line, Liquid, Page, Pie
from pyecharts. commons. utils import JsCode
from pyecharts. components import Table
from pyecharts. faker import Faker
def bar_datazoom_slider ( ) - > Bar:
c = (
Bar( )
. add_xaxis( Faker. days_attrs)
. add_yaxis( "商家A" , Faker. days_values)
. set_global_opts(
title_opts= opts. TitleOpts( title= "Bar-DataZoom(slider-水平)" ) ,
datazoom_opts= [ opts. DataZoomOpts( ) ] ,
)
)
return c
def line_markpoint ( ) - > Line:
c = (
Line( )
. add_xaxis( Faker. choose( ) )
. add_yaxis(
"商家A" ,
Faker. values( ) ,
markpoint_opts= opts. MarkPointOpts( data= [ opts. MarkPointItem( type_= "min" ) ] ) ,
)
. add_yaxis(
"商家B" ,
Faker. values( ) ,
markpoint_opts= opts. MarkPointOpts( data= [ opts. MarkPointItem( type_= "max" ) ] ) ,
)
. set_global_opts( title_opts= opts. TitleOpts( title= "Line-MarkPoint" ) )
)
return c
def pie_rosetype ( ) - > Pie:
v = Faker. choose( )
c = (
Pie( )
. add(
"" ,
[ list ( z) for z in zip ( v, Faker. values( ) ) ] ,
radius= [ "30%" , "75%" ] ,
center= [ "25%" , "50%" ] ,
rosetype= "radius" ,
label_opts= opts. LabelOpts( is_show= False ) ,
)
. add(
"" ,
[ list ( z) for z in zip ( v, Faker. values( ) ) ] ,
radius= [ "30%" , "75%" ] ,
center= [ "75%" , "50%" ] ,
rosetype= "area" ,
)
. set_global_opts( title_opts= opts. TitleOpts( title= "Pie-玫瑰图示例" ) )
)
return c
def grid_mutil_yaxis ( ) - > Grid:
x_data = [ "{}月" . format ( i) for i in range ( 1 , 13 ) ]
bar = (
Bar( )
. add_xaxis( x_data)
. add_yaxis(
"蒸发量" ,
[ 2.0 , 4.9 , 7.0 , 23.2 , 25.6 , 76.7 , 135.6 , 162.2 , 32.6 , 20.0 , 6.4 , 3.3 ] ,
yaxis_index= 0 ,
color= "#d14a61" ,
)
. add_yaxis(
"降水量" ,
[ 2.6 , 5.9 , 9.0 , 26.4 , 28.7 , 70.7 , 175.6 , 182.2 , 48.7 , 18.8 , 6.0 , 2.3 ] ,
yaxis_index= 1 ,
color= "#5793f3" ,
)
. extend_axis(
yaxis= opts. AxisOpts(
name= "蒸发量" ,
type_= "value" ,
min_= 0 ,
max_= 250 ,
position= "right" ,
axisline_opts= opts. AxisLineOpts(
linestyle_opts= opts. LineStyleOpts( color= "#d14a61" )
) ,
axislabel_opts= opts. LabelOpts( formatter= "{value} ml" ) ,
)
)
. extend_axis(
yaxis= opts. AxisOpts(
type_= "value" ,
name= "温度" ,
min_= 0 ,
max_= 25 ,
position= "left" ,
axisline_opts= opts. AxisLineOpts(
linestyle_opts= opts. LineStyleOpts( color= "#675bba" )
) ,
axislabel_opts= opts. LabelOpts( formatter= "{value} °C" ) ,
splitline_opts= opts. SplitLineOpts(
is_show= True , linestyle_opts= opts. LineStyleOpts( opacity= 1 )
) ,
)
)
. set_global_opts(
yaxis_opts= opts. AxisOpts(
name= "降水量" ,
min_= 0 ,
max_= 250 ,
position= "right" ,
offset= 80 ,
axisline_opts= opts. AxisLineOpts(
linestyle_opts= opts. LineStyleOpts( color= "#5793f3" )
) ,
axislabel_opts= opts. LabelOpts( formatter= "{value} ml" ) ,
) ,
title_opts= opts. TitleOpts( title= "Grid-多 Y 轴示例" ) ,
tooltip_opts= opts. TooltipOpts( trigger= "axis" , axis_pointer_type= "cross" ) ,
)
)
line = (
Line( )
. add_xaxis( x_data)
. add_yaxis(
"平均温度" ,
[ 2.0 , 2.2 , 3.3 , 4.5 , 6.3 , 10.2 , 20.3 , 23.4 , 23.0 , 16.5 , 12.0 , 6.2 ] ,
yaxis_index= 2 ,
color= "#675bba" ,
label_opts= opts. LabelOpts( is_show= False ) ,
)
)
bar. overlap( line)
return Grid( ) . add(
bar, opts. GridOpts( pos_left= "5%" , pos_right= "20%" ) , is_control_axis_index= True
)
def liquid_data_precision ( ) - > Liquid:
c = (
Liquid( )
. add(
"lq" ,
[ 0.3254 ] ,
label_opts= opts. LabelOpts(
font_size= 50 ,
formatter= JsCode(
"""function (param) {
return (Math.floor(param.value * 10000) / 100) + '%';
}"""
) ,
position= "inside" ,
) ,
)
. set_global_opts( title_opts= opts. TitleOpts( title= "Liquid-数据精度" ) )
)
return c
def table_base ( ) - > Table:
table = Table( )
headers = [ "City name" , "Area" , "Population" , "Annual Rainfall" ]
rows = [
[ "Brisbane" , 5905 , 1857594 , 1146.4 ] ,
[ "Adelaide" , 1295 , 1158259 , 600.5 ] ,
[ "Darwin" , 112 , 120900 , 1714.7 ] ,
[ "Hobart" , 1357 , 205556 , 619.5 ] ,
[ "Sydney" , 2058 , 4336374 , 1214.8 ] ,
[ "Melbourne" , 1566 , 3806092 , 646.9 ] ,
[ "Perth" , 5386 , 1554769 , 869.4 ] ,
]
table. add( headers, rows) . set_global_opts(
title_opts= opts. ComponentTitleOpts( title= "Table" )
)
return table
def page_draggable_layout ( ) :
page = Page( layout= Page. DraggablePageLayout)
page. add(
bar_datazoom_slider( ) ,
line_markpoint( ) ,
pie_rosetype( ) ,
grid_mutil_yaxis( ) ,
liquid_data_precision( ) ,
table_base( ) ,
)
page. render( "page_draggable_layout.html" )
if __name__ == "__main__" :
page_draggable_layout( )
88.3 Page - Page_simple_layout
链接
from pyecharts import options as opts
from pyecharts. charts import Bar, Grid, Line, Liquid, Page, Pie
from pyecharts. commons. utils import JsCode
from pyecharts. components import Table
from pyecharts. faker import Faker
def bar_datazoom_slider ( ) - > Bar:
c = (
Bar( )
. add_xaxis( Faker. days_attrs)
. add_yaxis( "商家A" , Faker. days_values)
. set_global_opts(
title_opts= opts. TitleOpts( title= "Bar-DataZoom(slider-水平)" ) ,
datazoom_opts= [ opts. DataZoomOpts( ) ] ,
)
)
return c
def line_markpoint ( ) - > Line:
c = (
Line( )
. add_xaxis( Faker. choose( ) )
. add_yaxis(
"商家A" ,
Faker. values( ) ,
markpoint_opts= opts. MarkPointOpts( data= [ opts. MarkPointItem( type_= "min" ) ] ) ,
)
. add_yaxis(
"商家B" ,
Faker. values( ) ,
markpoint_opts= opts. MarkPointOpts( data= [ opts. MarkPointItem( type_= "max" ) ] ) ,
)
. set_global_opts( title_opts= opts. TitleOpts( title= "Line-MarkPoint" ) )
)
return c
def pie_rosetype ( ) - > Pie:
v = Faker. choose( )
c = (
Pie( )
. add(
"" ,
[ list ( z) for z in zip ( v, Faker. values( ) ) ] ,
radius= [ "30%" , "75%" ] ,
center= [ "25%" , "50%" ] ,
rosetype= "radius" ,
label_opts= opts. LabelOpts( is_show= False ) ,
)
. add(
"" ,
[ list ( z) for z in zip ( v, Faker. values( ) ) ] ,
radius= [ "30%" , "75%" ] ,
center= [ "75%" , "50%" ] ,
rosetype= "area" ,
)
. set_global_opts( title_opts= opts. TitleOpts( title= "Pie-玫瑰图示例" ) )
)
return c
def grid_mutil_yaxis ( ) - > Grid:
x_data = [ "{}月" . format ( i) for i in range ( 1 , 13 ) ]
bar = (
Bar( )
. add_xaxis( x_data)
. add_yaxis(
"蒸发量" ,
[ 2.0 , 4.9 , 7.0 , 23.2 , 25.6 , 76.7 , 135.6 , 162.2 , 32.6 , 20.0 , 6.4 , 3.3 ] ,
yaxis_index= 0 ,
color= "#d14a61" ,
)
. add_yaxis(
"降水量" ,
[ 2.6 , 5.9 , 9.0 , 26.4 , 28.7 , 70.7 , 175.6 , 182.2 , 48.7 , 18.8 , 6.0 , 2.3 ] ,
yaxis_index= 1 ,
color= "#5793f3" ,
)
. extend_axis(
yaxis= opts. AxisOpts(
name= "蒸发量" ,
type_= "value" ,
min_= 0 ,
max_= 250 ,
position= "right" ,
axisline_opts= opts. AxisLineOpts(
linestyle_opts= opts. LineStyleOpts( color= "#d14a61" )
) ,
axislabel_opts= opts. LabelOpts( formatter= "{value} ml" ) ,
)
)
. extend_axis(
yaxis= opts. AxisOpts(
type_= "value" ,
name= "温度" ,
min_= 0 ,
max_= 25 ,
position= "left" ,
axisline_opts= opts. AxisLineOpts(
linestyle_opts= opts. LineStyleOpts( color= "#675bba" )
) ,
axislabel_opts= opts. LabelOpts( formatter= "{value} °C" ) ,
splitline_opts= opts. SplitLineOpts(
is_show= True , linestyle_opts= opts. LineStyleOpts( opacity= 1 )
) ,
)
)
. set_global_opts(
yaxis_opts= opts. AxisOpts(
name= "降水量" ,
min_= 0 ,
max_= 250 ,
position= "right" ,
offset= 80 ,
axisline_opts= opts. AxisLineOpts(
linestyle_opts= opts. LineStyleOpts( color= "#5793f3" )
) ,
axislabel_opts= opts. LabelOpts( formatter= "{value} ml" ) ,
) ,
title_opts= opts. TitleOpts( title= "Grid-多 Y 轴示例" ) ,
tooltip_opts= opts. TooltipOpts( trigger= "axis" , axis_pointer_type= "cross" ) ,
)
)
line = (
Line( )
. add_xaxis( x_data)
. add_yaxis(
"平均温度" ,
[ 2.0 , 2.2 , 3.3 , 4.5 , 6.3 , 10.2 , 20.3 , 23.4 , 23.0 , 16.5 , 12.0 , 6.2 ] ,
yaxis_index= 2 ,
color= "#675bba" ,
label_opts= opts. LabelOpts( is_show= False ) ,
)
)
bar. overlap( line)
return Grid( ) . add(
bar, opts. GridOpts( pos_left= "5%" , pos_right= "20%" ) , is_control_axis_index= True
)
def liquid_data_precision ( ) - > Liquid:
c = (
Liquid( )
. add(
"lq" ,
[ 0.3254 ] ,
label_opts= opts. LabelOpts(
font_size= 50 ,
formatter= JsCode(
"""function (param) {
return (Math.floor(param.value * 10000) / 100) + '%';
}"""
) ,
position= "inside" ,
) ,
)
. set_global_opts( title_opts= opts. TitleOpts( title= "Liquid-数据精度" ) )
)
return c
def table_base ( ) - > Table:
table = Table( )
headers = [ "City name" , "Area" , "Population" , "Annual Rainfall" ]
rows = [
[ "Brisbane" , 5905 , 1857594 , 1146.4 ] ,
[ "Adelaide" , 1295 , 1158259 , 600.5 ] ,
[ "Darwin" , 112 , 120900 , 1714.7 ] ,
[ "Hobart" , 1357 , 205556 , 619.5 ] ,
[ "Sydney" , 2058 , 4336374 , 1214.8 ] ,
[ "Melbourne" , 1566 , 3806092 , 646.9 ] ,
[ "Perth" , 5386 , 1554769 , 869.4 ] ,
]
table. add( headers, rows) . set_global_opts(
title_opts= opts. ComponentTitleOpts( title= "Table" )
)
return table
def page_simple_layout ( ) :
page = Page( layout= Page. SimplePageLayout)
page. add(
bar_datazoom_slider( ) ,
line_markpoint( ) ,
pie_rosetype( ) ,
grid_mutil_yaxis( ) ,
liquid_data_precision( ) ,
table_base( ) ,
)
page. render( "page_simple_layout.html" )
if __name__ == "__main__" :
page_simple_layout( )
89.Tab:选项卡多图
链接
class pyecharts.charts.Tab
class Tab (
page_title: str = "Awesome-pyecharts" ,
js_host: str = ""
)
func pyecharts.charts.Tab.add
def add (
chart,
tab_name
) :
89.1 Tab - Tab_base
链接
from pyecharts import options as opts
from pyecharts. charts import Bar, Grid, Line, Pie, Tab
from pyecharts. faker import Faker
def bar_datazoom_slider ( ) - > Bar:
c = (
Bar( )
. add_xaxis( Faker. days_attrs)
. add_yaxis( "商家A" , Faker. days_values)
. set_global_opts(
title_opts= opts. TitleOpts( title= "Bar-DataZoom(slider-水平)" ) ,
datazoom_opts= [ opts. DataZoomOpts( ) ] ,
)
)
return c
def line_markpoint ( ) - > Line:
c = (
Line( )
. add_xaxis( Faker. choose( ) )
. add_yaxis(
"商家A" ,
Faker. values( ) ,
markpoint_opts= opts. MarkPointOpts( data= [ opts. MarkPointItem( type_= "min" ) ] ) ,
)
. add_yaxis(
"商家B" ,
Faker. values( ) ,
markpoint_opts= opts. MarkPointOpts( data= [ opts. MarkPointItem( type_= "max" ) ] ) ,
)
. set_global_opts( title_opts= opts. TitleOpts( title= "Line-MarkPoint" ) )
)
return c
def pie_rosetype ( ) - > Pie:
v = Faker. choose( )
c = (
Pie( )
. add(
"" ,
[ list ( z) for z in zip ( v, Faker. values( ) ) ] ,
radius= [ "30%" , "75%" ] ,
center= [ "25%" , "50%" ] ,
rosetype= "radius" ,
label_opts= opts. LabelOpts( is_show= False ) ,
)
. add(
"" ,
[ list ( z) for z in zip ( v, Faker. values( ) ) ] ,
radius= [ "30%" , "75%" ] ,
center= [ "75%" , "50%" ] ,
rosetype= "area" ,
)
. set_global_opts( title_opts= opts. TitleOpts( title= "Pie-玫瑰图示例" ) )
)
return c
def grid_mutil_yaxis ( ) - > Grid:
x_data = [ "{}月" . format ( i) for i in range ( 1 , 13 ) ]
bar = (
Bar( )
. add_xaxis( x_data)
. add_yaxis(
"蒸发量" ,
[ 2.0 , 4.9 , 7.0 , 23.2 , 25.6 , 76.7 , 135.6 , 162.2 , 32.6 , 20.0 , 6.4 , 3.3 ] ,
yaxis_index= 0 ,
color= "#d14a61" ,
)
. add_yaxis(
"降水量" ,
[ 2.6 , 5.9 , 9.0 , 26.4 , 28.7 , 70.7 , 175.6 , 182.2 , 48.7 , 18.8 , 6.0 , 2.3 ] ,
yaxis_index= 1 ,
color= "#5793f3" ,
)
. extend_axis(
yaxis= opts. AxisOpts(
name= "蒸发量" ,
type_= "value" ,
min_= 0 ,
max_= 250 ,
position= "right" ,
axisline_opts= opts. AxisLineOpts(
linestyle_opts= opts. LineStyleOpts( color= "#d14a61" )
) ,
axislabel_opts= opts. LabelOpts( formatter= "{value} ml" ) ,
)
)
. extend_axis(
yaxis= opts. AxisOpts(
type_= "value" ,
name= "温度" ,
min_= 0 ,
max_= 25 ,
position= "left" ,
axisline_opts= opts. AxisLineOpts(
linestyle_opts= opts. LineStyleOpts( color= "#675bba" )
) ,
axislabel_opts= opts. LabelOpts( formatter= "{value} °C" ) ,
splitline_opts= opts. SplitLineOpts(
is_show= True , linestyle_opts= opts. LineStyleOpts( opacity= 1 )
) ,
)
)
. set_global_opts(
yaxis_opts= opts. AxisOpts(
name= "降水量" ,
min_= 0 ,
max_= 250 ,
position= "right" ,
offset= 80 ,
axisline_opts= opts. AxisLineOpts(
linestyle_opts= opts. LineStyleOpts( color= "#5793f3" )
) ,
axislabel_opts= opts. LabelOpts( formatter= "{value} ml" ) ,
) ,
title_opts= opts. TitleOpts( title= "Grid-多 Y 轴示例" ) ,
tooltip_opts= opts. TooltipOpts( trigger= "axis" , axis_pointer_type= "cross" ) ,
)
)
line = (
Line( )
. add_xaxis( x_data)
. add_yaxis(
"平均温度" ,
[ 2.0 , 2.2 , 3.3 , 4.5 , 6.3 , 10.2 , 20.3 , 23.4 , 23.0 , 16.5 , 12.0 , 6.2 ] ,
yaxis_index= 2 ,
color= "#675bba" ,
label_opts= opts. LabelOpts( is_show= False ) ,
)
)
bar. overlap( line)
return Grid( ) . add(
bar, opts. GridOpts( pos_left= "5%" , pos_right= "20%" ) , is_control_axis_index= True
)
tab = Tab( )
tab. add( bar_datazoom_slider( ) , "bar-example" )
tab. add( line_markpoint( ) , "line-example" )
tab. add( pie_rosetype( ) , "pie-example" )
tab. add( grid_mutil_yaxis( ) , "grid-example" )
tab. render( "tab_base.html" )
?89.Timeline:时间线轮播多图
链接
class pyecharts.charts.Timeline
class Timeline (
init_opts: opts. InitOpts = opts. InitOpts( )
)
func pyecharts.charts.Timeline.add_schema
def add_schema (
axis_type: str = "category" ,
orient: str = "horizontal" ,
symbol: Optional[ str ] = None ,
symbol_size: Optional[ Numeric] = None ,
play_interval: Optional[ Numeric] = None ,
control_position: str = "left" ,
is_auto_play: bool = False ,
is_loop_play: bool = True ,
is_rewind_play: bool = False ,
is_timeline_show: bool = True ,
is_inverse: bool = False ,
pos_left: Optional[ str ] = None ,
pos_right: Optional[ str ] = None ,
pos_top: Optional[ str ] = None ,
pos_bottom: Optional[ str ] = "-5px" ,
width: Optional[ str ] = None ,
height: Optional[ str ] = None ,
linestyle_opts: Union[ opts. LineStyleOpts, dict , None ] = None ,
label_opts: Optional[ opts. LabelOpts] = None ,
itemstyle_opts: Union[ opts. ItemStyleOpts, dict , None ] = None ,
graphic_opts: types. Graphic = None ,
checkpointstyle_opts: types. TimeLinkCheckPoint = None ,
controlstyle_opts: types. TimeLineControl = None ,
)
func pyecharts.charts.Timeline.add
def add (
chart: Base,
time_point: str
)
TimeLinkCheckPoint: 时间轴 checkpoint 样式配置
class TimelineCheckPointerStyle (
symbol: str = "circle" ,
symbol_size: Union[ Numeric, Sequence[ Numeric] ] = 13 ,
symbol_rotate: Optional[ Numeric] = None ,
symbol_keep_aspect: bool = False ,
symbol_offset: Optional[ Sequence[ Union[ str , Numeric] ] ] = None ,
color: str = "#c23531" ,
border_width: Numeric = 5 ,
border_color: str = "rgba(194,53,49,0.5)" ,
is_animation: bool = True ,
animation_duration: Numeric = 300 ,
animation_easing: str = "quinticInOut" ,
)
TimelineControlStyle: 时间轴控制按钮样式
class TimelineControlStyle (
is_show: bool = True ,
is_show_play_button: bool = True ,
is_show_prev_button: bool = True ,
is_show_next_button: bool = True ,
item_size: Numeric = 22 ,
item_gap: Numeric = 12 ,
position: str = "left" ,
play_icon: Optional[ str ] = None ,
stop_icon: Optional[ str ] = None ,
prev_icon: Optional[ str ] = None ,
next_icon: Optional[ str ] = None ,
color: str = "#304654" ,
border_color: str = "#304654" ,
border_width: Numeric = 1 ,
)
89.1 Timeline - Timeline_pie
链接
from pyecharts import options as opts
from pyecharts. charts import Pie, Timeline
from pyecharts. faker import Faker
attr = Faker. choose( )
tl = Timeline( )
for i in range ( 2015 , 2020 ) :
pie = (
Pie( )
. add(
"商家A" ,
[ list ( z) for z in zip ( attr, Faker. values( ) ) ] ,
rosetype= "radius" ,
radius= [ "30%" , "55%" ] ,
)
. set_global_opts( title_opts= opts. TitleOpts( "某商店{}年营业额" . format ( i) ) )
)
tl. add( pie, "{}年" . format ( i) )
tl. render( "timeline_pie.html" )
?89.2 Timeline - Timeline_bar_with_graphic
链接
from pyecharts import options as opts
from pyecharts. charts import Bar, Timeline
from pyecharts. commons. utils import JsCode
from pyecharts. faker import Faker
x = Faker. choose( )
tl = Timeline( )
for i in range ( 2015 , 2020 ) :
bar = (
Bar( )
. add_xaxis( x)
. add_yaxis( "商家A" , Faker. values( ) )
. add_yaxis( "商家B" , Faker. values( ) )
. set_global_opts(
title_opts= opts. TitleOpts( "某商店{}年营业额 - With Graphic 组件" . format ( i) ) ,
graphic_opts= [
opts. GraphicGroup(
graphic_item= opts. GraphicItem(
rotation= JsCode( "Math.PI / 4" ) ,
bounding= "raw" ,
right= 100 ,
bottom= 110 ,
z= 100 ,
) ,
children= [
opts. GraphicRect(
graphic_item= opts. GraphicItem(
left= "center" , top= "center" , z= 100
) ,
graphic_shape_opts= opts. GraphicShapeOpts(
width= 400 , height= 50
) ,
graphic_basicstyle_opts= opts. GraphicBasicStyleOpts(
fill= "rgba(0,0,0,0.3)"
) ,
) ,
opts. GraphicText(
graphic_item= opts. GraphicItem(
left= "center" , top= "center" , z= 100
) ,
graphic_textstyle_opts= opts. GraphicTextStyleOpts(
text= "某商店{}年营业额" . format ( i) ,
font= "bold 26px Microsoft YaHei" ,
graphic_basicstyle_opts= opts. GraphicBasicStyleOpts(
fill= "#fff"
) ,
) ,
) ,
] ,
)
] ,
)
)
tl. add( bar, "{}年" . format ( i) )
tl. render( "timeline_bar_with_graphic.html" )
89.3 Timeline - Timeline_bmap
链接
from pyecharts import options as opts
from pyecharts. charts import BMap, Timeline
from pyecharts. faker import Faker
tl = Timeline( )
tl. add_schema( pos_left= "50%" , pos_right= "10px" , pos_bottom= "15px" )
for i in range ( 2015 , 2020 ) :
bmap = (
BMap( )
. add_schema( baidu_ak= "FAKE_AK" , center= [ 120.13066322374 , 30.240018034923 ] )
. add(
"bmap" ,
[ list ( z) for z in zip ( Faker. provinces, Faker. values( ) ) ] ,
type_= "heatmap" ,
)
. set_global_opts(
title_opts= opts. TitleOpts( title= "Timeline-BMap-热力图-{}年" . format ( i) ) ,
visualmap_opts= opts. VisualMapOpts( pos_bottom= "center" , pos_right= "10px" ) ,
tooltip_opts= opts. TooltipOpts( formatter= None ) ,
)
)
tl. add( bmap, "{}年" . format ( i) )
tl. render( "timeline_bmap.html" )
89.4 Timeline - Timeline_sankey
链接
from pyecharts import options as opts
from pyecharts. charts import Sankey, Timeline
from pyecharts. faker import Faker
tl = Timeline( )
names = ( "商家A" , "商家B" , "商家C" )
nodes = [ { "name" : name} for name in names]
for i in range ( 2015 , 2020 ) :
links = [
{ "source" : names[ 0 ] , "target" : names[ 1 ] , "value" : Faker. values( ) [ 0 ] } ,
{ "source" : names[ 1 ] , "target" : names[ 2 ] , "value" : Faker. values( ) [ 0 ] } ,
]
sankey = (
Sankey( )
. add(
"sankey" ,
nodes,
links,
linestyle_opt= opts. LineStyleOpts( opacity= 0.2 , curve= 0.5 , color= "source" ) ,
label_opts= opts. LabelOpts( position= "right" ) ,
)
. set_global_opts(
title_opts= opts. TitleOpts( title= "{}年商店(A, B, C)营业额差" . format ( i) )
)
)
tl. add( sankey, "{}年" . format ( i) )
tl. render( "timeline_sankey.html" )
89.5 Timeline - Timeline_bar_reversal
链接
from pyecharts import options as opts
from pyecharts. charts import Bar, Timeline
from pyecharts. faker import Faker
tl = Timeline( )
for i in range ( 2015 , 2020 ) :
bar = (
Bar( )
. add_xaxis( Faker. choose( ) )
. add_yaxis( "商家A" , Faker. values( ) , label_opts= opts. LabelOpts( position= "right" ) )
. add_yaxis( "商家B" , Faker. values( ) , label_opts= opts. LabelOpts( position= "right" ) )
. reversal_axis( )
. set_global_opts(
title_opts= opts. TitleOpts( "Timeline-Bar-Reversal (时间: {} 年)" . format ( i) )
)
)
tl. add( bar, "{}年" . format ( i) )
tl. render( "timeline_bar_reversal.html" )
89.6 Timeline - Timeline_bar
链接
from pyecharts import options as opts
from pyecharts. charts import Bar, Timeline
from pyecharts. faker import Faker
x = Faker. choose( )
tl = Timeline( )
for i in range ( 2015 , 2020 ) :
bar = (
Bar( )
. add_xaxis( x)
. add_yaxis( "商家A" , Faker. values( ) )
. add_yaxis( "商家B" , Faker. values( ) )
. set_global_opts( title_opts= opts. TitleOpts( "某商店{}年营业额" . format ( i) ) )
)
tl. add( bar, "{}年" . format ( i) )
tl. render( "timeline_bar.html" )
89.7 Timeline - Timeline_multi_axis
链接
from pyecharts import options as opts
from pyecharts. charts import Bar, Timeline
from pyecharts. faker import Faker
tl = Timeline( )
for i in range ( 2015 , 2020 ) :
bar = (
Bar( )
. add_xaxis( Faker. choose( ) )
. add_yaxis( "商家A" , Faker. values( ) )
. add_yaxis( "商家B" , Faker. values( ) )
. set_global_opts( title_opts= opts. TitleOpts( "某商店{}年营业额" . format ( i) ) )
)
tl. add( bar, "{}年" . format ( i) )
tl. render( "timeline_multi_axis.html" )
89.8 Timeline - Timeline_map
链接
from pyecharts import options as opts
from pyecharts. charts import Map, Timeline
from pyecharts. faker import Faker
tl = Timeline( )
for i in range ( 2015 , 2020 ) :
map0 = (
Map( )
. add( "商家A" , [ list ( z) for z in zip ( Faker. provinces, Faker. values( ) ) ] , "china" )
. set_global_opts(
title_opts= opts. TitleOpts( title= "Map-{}年某些数据" . format ( i) ) ,
visualmap_opts= opts. VisualMapOpts( max_= 200 ) ,
)
)
tl. add( map0, "{}年" . format ( i) )
tl. render( "timeline_map.html" )