0.导入数据
import numpy as np
import pandas as pd
import pymysql
from pyecharts import options as opts
from pyecharts. charts import Bar, Line, Grid
from pyecharts. commons. utils import JsCode
from pyecharts. globals import ThemeType
conn = pymysql. connect(
host = '127.0.0.1' ,
user = 'root' ,
password = '123' ,
database = 'cloudmusic' ,
charset = 'utf8'
)
df = pd. read_sql( "select * from playlists" , con= conn)
year_df = df. groupby( df[ 'create_time' ] . str [ : 4 ] ) . sum ( )
year_df
year_df = year_df. loc[ '2013' : , : ]
1.基本折线图
(
Line( )
. add_xaxis( xaxis_data= year_df. index. tolist( ) )
. add_yaxis(
series_name = "歌曲数" ,
y_axis = year_df. tracks_num. tolist( ) ,
symbol = "emptyCircle" ,
is_symbol_show = True ,
label_opts = opts. LabelOpts( is_show= False ) ,
)
. set_global_opts(
xaxis_opts = opts. AxisOpts( type_= "category" ) ,
yaxis_opts = opts. AxisOpts(
type_ = "value" ,
axistick_opts = opts. AxisTickOpts( is_show= True ) ,
splitline_opts = opts. SplitLineOpts( is_show= True ) ,
) ,
title_opts = opts. TitleOpts( title= "基本折线图" )
)
. render_notebook( )
)
2.Log折线图
(
Line( init_opts = opts. InitOpts( width= "1000px" ) )
. add_xaxis( xaxis_data= year_df. index. tolist( ) )
. add_yaxis(
series_name = "log(播放量)" ,
y_axis = year_df. play_count. tolist( ) ,
linestyle_opts = opts. LineStyleOpts( width= 2 )
)
. add_yaxis(
series_name = "log(收藏量)" ,
y_axis = year_df. subscribed_count. tolist( ) ,
linestyle_opts = opts. LineStyleOpts( width= 2 )
)
. set_global_opts(
title_opts = opts. TitleOpts( title= "对数轴" ) ,
xaxis_opts = opts. AxisOpts( type_= "category" , name= "x" ) ,
yaxis_opts = opts. AxisOpts(
type_= "log" ,
name= "y" ,
splitline_opts = opts. SplitLineOpts( is_show= True ) ,
is_scale = True ,
) ,
)
. render_notebook( )
)
3.双折线图
(
Line( )
. add_xaxis( xaxis_data= year_df. index. tolist( ) )
. add_yaxis(
series_name = "分享量" ,
y_axis = year_df. share_count. tolist( ) ,
is_smooth = True ,
markpoint_opts = opts. MarkPointOpts(
data = [
opts. MarkLineItem( type_= "max" , name= "最大值" , symbol_size= 100 ) ,
opts. MarkLineItem( type_= "min" , name= "最小值" , symbol_size= 100 ) ,
]
) ,
markline_opts = opts. MarkLineOpts(
data = [ opts. MarkLineItem( type_= "average" , name= "平均值" ) ]
) ,
)
. add_yaxis(
series_name = "评论数" ,
y_axis = year_df. comment_count. tolist( ) ,
is_smooth = True ,
markline_opts = opts. MarkLineOpts(
data = [
opts. MarkLineItem( type_= "average" , name= "平均值" ) ,
opts. MarkLineItem( symbol= "none" , x= "90%" , y= "max" ) ,
opts. MarkLineItem( type_= "max" , name= "最高点" ) ,
]
) ,
)
. set_global_opts(
title_opts = opts. TitleOpts( title= "堆叠折线图" ) ,
tooltip_opts = opts. TooltipOpts( trigger= "axis" ) ,
toolbox_opts = opts. ToolboxOpts( is_show= True ) ,
xaxis_opts = opts. AxisOpts( type_= "category" , boundary_gap= False ) ,
)
. render_notebook( )
)
4.面积图
(
Line( )
. add_xaxis( year_df. index. tolist( ) )
. add_yaxis( "分享量" , year_df. share_count. tolist( ) , is_smooth= True )
. add_yaxis( "评论数" , year_df. comment_count. tolist( ) , is_smooth= True )
. set_global_opts(
title_opts = opts. TitleOpts( title= "面积图-紧贴Y轴" ) ,
xaxis_opts = opts. AxisOpts(
axistick_opts = opts. AxisTickOpts( is_align_with_label= True ) ,
is_scale = False ,
boundary_gap = False ,
)
)
. set_series_opts(
areastyle_opts = opts. AreaStyleOpts( opacity= 0.5 ) ,
label_opts = opts. LabelOpts( is_show= False ) ,
)
. render_notebook( )
)
5.颜色渐变折线图
background_color_js = (
"new echarts.graphic.LinearGradient(0,0,0,1,"
"[{offset: 0, color: '#c86589'}, {offset: 1, color: '#06a7ff'}], false)"
)
area_color_js = (
"new echarts.graphic.LinearGradient(0,0,0,1,"
"[{offset: 0, color: '#eb64fb'}, {offset: 1, color: '#3fbbff0d'}], false)"
)
line = (
Line( init_opts= opts. InitOpts( bg_color= JsCode( background_color_js) ) )
. add_xaxis( xaxis_data= year_df. index. tolist( ) )
. add_yaxis(
series_name = "歌曲数" ,
y_axis = year_df. tracks_num. tolist( ) ,
is_smooth = True ,
is_symbol_show = True ,
symbol = "circle" ,
symbol_size = 6 ,
linestyle_opts = opts. LineStyleOpts( color= "#fff" ) ,
label_opts = opts. LabelOpts( is_show= True , position= "top" , color= "white" ) ,
itemstyle_opts= opts. ItemStyleOpts(
color= "red" , border_color= "#fff" , border_width= 3
) ,
tooltip_opts = opts. TooltipOpts( is_show= False ) ,
areastyle_opts = opts. AreaStyleOpts( color= JsCode( area_color_js) , opacity= 1 ) ,
)
. set_global_opts(
title_opts = opts. TitleOpts(
title = "歌单歌曲数" ,
pos_bottom = "5%" ,
pos_left = "center" ,
title_textstyle_opts = opts. TextStyleOpts( color= "#fff" , font_size= 16 ) ,
) ,
xaxis_opts = opts. AxisOpts(
type_ = "category" ,
boundary_gap = False ,
axislabel_opts = opts. LabelOpts( margin= 30 , color= "#ffffff63" ) ,
axisline_opts = opts. AxisLineOpts( is_show= False ) ,
axistick_opts = opts. AxisTickOpts(
is_show = True ,
length = 25 ,
linestyle_opts = opts. LineStyleOpts( color= "#ffffff1f" ) ,
) ,
splitline_opts = opts. SplitLineOpts(
is_show= True , linestyle_opts= opts. LineStyleOpts( color= "#ffffff1f" )
) ,
) ,
yaxis_opts = opts. AxisOpts(
type_ = "value" ,
position = "right" ,
axislabel_opts = opts. LabelOpts( margin= 20 , color= "#ffffff63" ) ,
axisline_opts = opts. AxisLineOpts(
linestyle_opts = opts. LineStyleOpts( width= 2 , color= "#fff" )
) ,
axistick_opts = opts. AxisTickOpts(
is_show = True ,
length = 15 ,
linestyle_opts = opts. LineStyleOpts( color= "#ffffff1f" )
) ,
splitline_opts = opts. SplitLineOpts(
is_show= True , linestyle_opts= opts. LineStyleOpts( color= "#ffffff1f" )
) ,
) ,
legend_opts = opts. LegendOpts( is_show= False ) ,
)
)
(
Grid( )
. add(
line,
grid_opts = opts. GridOpts(
pos_top = "20%" ,
pos_left = "10%" ,
pos_right = "10%" ,
pos_bottom = "15%" ,
is_contain_label = True ,
) ,
)
. render_notebook( )
)
6.区域颜色折线图
df2019 = df[ df[ 'create_time' ] . str [ : 4 ] == "2019" ]
month2019 = df2019. groupby( df2019[ "create_time" ] . str [ 5 : 7 ] ) . count( ) [ "id" ]
(
Line( )
. add_xaxis( xaxis_data= month2019. index. tolist( ) )
. add_yaxis(
series_name = "歌单数" ,
y_axis = month2019. tolist( ) ,
is_smooth = True ,
label_opts = opts. LabelOpts( is_show= False ) ,
linestyle_opts = opts. LineStyleOpts( width= 2 ) ,
)
. set_global_opts(
title_opts = opts. TitleOpts( title= "2019年创建的歌单数" ) ,
tooltip_opts = opts. TooltipOpts( trigger= "axis" , axis_pointer_type= "cross" ) ,
xaxis_opts = opts. AxisOpts( boundary_gap= False ) ,
yaxis_opts = opts. AxisOpts(
axislabel_opts = opts. LabelOpts( formatter= "{value} W" ) ,
splitline_opts = opts. SplitLineOpts( is_show= True ) ,
) ,
visualmap_opts = opts. VisualMapOpts(
is_piecewise = True ,
dimension = 0 ,
pieces = [
{ "lte" : 2 , "color" : "#f47a75" } ,
{ "gt" : 2 , "lte" : 6 , "color" : "#009db2" } ,
{ "gt" : 6 , "lte" : 8 , "color" : "#f47a75" } ,
{ "gt" : 8 , "color" : "#009db2" } ,
] ,
) ,
)
. set_series_opts(
markarea_opts = opts. MarkAreaOpts(
data = [
opts. MarkAreaItem( name= "冬季" , x= ( 0 , 2 ) ) ,
opts. MarkAreaItem( name= "夏季" , x= ( 6 , 8 ) ) ,
]
)
)
. render_notebook( )
)
7.堆积面积图
df[ df[ 'create_time' ] . str [ : 4 ] == "2015" ] . groupby( df[ df[ 'create_time' ] . str [ : 4 ] == "2015" ] [ "create_time" ] . str [ 5 : 7 ] ) . count( ) [ "id" ]
(
Line( )
. add_xaxis( xaxis_data= month2019. index. tolist( ) )
. add_yaxis(
series_name= "2015" ,
stack= "总量" ,
y_axis= df[ df[ 'create_time' ] . str [ : 4 ] == "2015" ] . groupby( df[ df[ 'create_time' ] . str [ : 4 ] == "2015" ] [ "create_time" ] . str [ 5 : 7 ] ) . count( ) [ "id" ] . tolist( ) ,
areastyle_opts= opts. AreaStyleOpts( opacity= 0.5 ) ,
label_opts= opts. LabelOpts( is_show= False ) ,
)
. add_yaxis(
series_name= "2016" ,
stack= "总量" ,
y_axis= df[ df[ 'create_time' ] . str [ : 4 ] == "2016" ] . groupby( df[ df[ 'create_time' ] . str [ : 4 ] == "2016" ] [ "create_time" ] . str [ 5 : 7 ] ) . count( ) [ "id" ] . tolist( ) ,
areastyle_opts= opts. AreaStyleOpts( opacity= 0.5 ) ,
label_opts= opts. LabelOpts( is_show= False ) ,
)
. add_yaxis(
series_name= "2017" ,
stack= "总量" ,
y_axis= df[ df[ 'create_time' ] . str [ : 4 ] == "2017" ] . groupby( df[ df[ 'create_time' ] . str [ : 4 ] == "2017" ] [ "create_time" ] . str [ 5 : 7 ] ) . count( ) [ "id" ] . tolist( ) ,
areastyle_opts= opts. AreaStyleOpts( opacity= 0.5 ) ,
label_opts= opts. LabelOpts( is_show= False ) ,
)
. add_yaxis(
series_name= "2018" ,
stack= "总量" ,
y_axis= df[ df[ 'create_time' ] . str [ : 4 ] == "2018" ] . groupby( df[ df[ 'create_time' ] . str [ : 4 ] == "2018" ] [ "create_time" ] . str [ 5 : 7 ] ) . count( ) [ "id" ] . tolist( ) ,
areastyle_opts= opts. AreaStyleOpts( opacity= 0.5 ) ,
label_opts= opts. LabelOpts( is_show= False ) ,
)
. add_yaxis(
series_name= "2019" ,
stack= "总量" ,
y_axis= df[ df[ 'create_time' ] . str [ : 4 ] == "2019" ] . groupby( df[ df[ 'create_time' ] . str [ : 4 ] == "2019" ] [ "create_time" ] . str [ 5 : 7 ] ) . count( ) [ "id" ] . tolist( ) ,
areastyle_opts= opts. AreaStyleOpts( opacity= 0.5 ) ,
label_opts= opts. LabelOpts( is_show= False ) ,
)
. set_global_opts(
title_opts= opts. TitleOpts( title= "堆叠区域图" ) ,
tooltip_opts= opts. TooltipOpts( trigger= "axis" , axis_pointer_type= "cross" ) ,
yaxis_opts= opts. AxisOpts(
type_= "value" ,
axistick_opts= opts. AxisTickOpts( is_show= True ) ,
splitline_opts= opts. SplitLineOpts( is_show= True ) ,
) ,
xaxis_opts= opts. AxisOpts( type_= "category" , boundary_gap= False ) ,
)
. render_notebook( )
)
8.双轴折线图
df[ df[ "create_time" ] . str [ : 4 ] == "2019" ] . groupby( df[ "create_time" ] . str [ : 7 ] ) . count( ) [ "id" ]
js_formatter = """function (params) {
console.log(params);
return '歌单数 ' + params.value + (params.seriesData.length ? ':' + params.seriesData[0].data : '');
}"""
(
Line( )
. add_xaxis(
xaxis_data = df[ df[ "create_time" ] . str [ : 4 ] == "2019" ] . groupby( df[ "create_time" ] . str [ : 7 ] ) . count( ) [ "id" ] . index. tolist( ) ,
)
. extend_axis(
xaxis_data = df[ df[ "create_time" ] . str [ : 4 ] == "2018" ] . groupby( df[ "create_time" ] . str [ : 7 ] ) . count( ) [ "id" ] . index. tolist( ) ,
xaxis = opts. AxisOpts(
type_ = "category" ,
axistick_opts = opts. AxisTickOpts( is_align_with_label= True ) ,
axisline_opts = opts. AxisLineOpts(
is_on_zero= False , linestyle_opts= opts. LineStyleOpts( color= "#6e9ef1" )
) ,
axispointer_opts = opts. AxisPointerOpts(
is_show= True , label= opts. LabelOpts( formatter= JsCode( js_formatter) )
) ,
) ,
)
. add_yaxis(
series_name = "2018年歌单数" ,
y_axis = df[ df[ "create_time" ] . str [ : 4 ] == "2018" ] . groupby( df[ "create_time" ] . str [ : 7 ] ) . count( ) [ "id" ] . tolist( ) ,
is_smooth = True ,
symbol = "emptyCircle" ,
is_symbol_show = False ,
color = "#d14a61" ,
label_opts = opts. LabelOpts( is_show= False ) ,
linestyle_opts = opts. LineStyleOpts( width= 2 ) ,
)
. add_yaxis(
series_name = "2019年歌单数" ,
y_axis = df[ df[ "create_time" ] . str [ : 4 ] == "2019" ] . groupby( df[ "create_time" ] . str [ : 7 ] ) . count( ) [ "id" ] . tolist( ) ,
is_smooth = True ,
symbol = "emptyCircle" ,
is_symbol_show = False ,
color = "#6e9ef1" ,
label_opts = opts. LabelOpts( is_show= False ) ,
linestyle_opts = opts. LineStyleOpts( width= 2 ) ,
)
. set_global_opts(
legend_opts= opts. LegendOpts( ) ,
tooltip_opts= opts. TooltipOpts( trigger= "none" , axis_pointer_type= "cross" ) ,
xaxis_opts= opts. AxisOpts(
type_= "category" ,
axistick_opts= opts. AxisTickOpts( is_align_with_label= True ) ,
axisline_opts= opts. AxisLineOpts(
is_on_zero= False , linestyle_opts= opts. LineStyleOpts( color= "#d14a61" )
) ,
axispointer_opts= opts. AxisPointerOpts(
is_show= True , label= opts. LabelOpts( formatter= JsCode( js_formatter) )
) ,
) ,
yaxis_opts= opts. AxisOpts(
type_= "value" ,
splitline_opts= opts. SplitLineOpts(
is_show= True , linestyle_opts= opts. LineStyleOpts( opacity= 1 )
) ,
) ,
)
. render_notebook( )
)