1. 程式人生 > 其它 >pycharts-從0開始美化一個圖表

pycharts-從0開始美化一個圖表

目標效果

這個是Echarts官方的視覺化作品, 點我跳轉,今天我們就嘗試下通過pyecharts能否實現一樣的效果~

資料來源

每項資料包含5個值,分別代表人均GDP,人均壽命,GDP總量,國家,年份

# 1990 & 2015年各國GDP&壽命

data = [[[28604, 77, 17096869, 'Australia', 1990],
         [31163, 77.4, 27662440, 'Canada', 1990],
         [1516, 68, 1154605773, 'China', 1990],
         [13670, 74.7, 10582082, 'Cuba', 1990],
         [28599, 75, 4986705, 'Finland', 1990],
         [29476, 77.1, 56943299, 'France', 1990],
         [31476, 75.4, 78958237, 'Germany', 1990],
         [28666, 78.1, 254830, 'Iceland', 1990],
         [1777, 57.7, 870601776, 'India', 1990],
         [29550, 79.1, 122249285, 'Japan', 1990],
         [2076, 67.9, 20194354, 'North Korea', 1990],
         [12087, 72, 42972254, 'South Korea', 1990],
         [24021, 75.4, 3397534, 'New Zealand', 1990],
         [43296, 76.8, 4240375, 'Norway', 1990],
         [10088, 70.8, 38195258, 'Poland', 1990],
         [19349, 69.6, 147568552, 'Russia', 1990],
         [10670, 67.3, 53994605, 'Turkey', 1990],
         [26424, 75.7, 57110117, 'United Kingdom', 1990],
         [37062, 75.4, 252847810, 'United States', 1990]],
        [[44056, 81.8, 23968973, 'Australia', 2015],
         [43294, 81.7, 35939927, 'Canada', 2015],
         [13334, 76.9, 1376048943, 'China', 2015],
         [21291, 78.5, 11389562, 'Cuba', 2015],
         [38923, 80.8, 5503457, 'Finland', 2015],
         [37599, 81.9, 64395345, 'France', 2015],
         [44053, 81.1, 80688545, 'Germany', 2015],
         [42182, 82.8, 329425, 'Iceland', 2015],
         [5903, 66.8, 1311050527, 'India', 2015],
         [36162, 83.5, 126573481, 'Japan', 2015],
         [1390, 71.4, 25155317, 'North Korea', 2015],
         [34644, 80.7, 50293439, 'South Korea', 2015],
         [34186, 80.6, 4528526, 'New Zealand', 2015],
         [64304, 81.6, 5210967, 'Norway', 2015],
         [24787, 77.3, 38611794, 'Poland', 2015],
         [23038, 73.13, 143456918, 'Russia', 2015],
         [19360, 76.5, 78665830, 'Turkey', 2015],
         [38225, 81.4, 64715810, 'United Kingdom', 2015],
         [53354, 79.1, 321773631, 'United States', 2015]]]
畫一個散點圖

先畫一個散點圖來展示1990年的資料~

為什麼不一起新增1990年和2015年的資料呢?

因為在直角座標系資料中,你必須公用一個x軸的資料才能一起新增
這兩個年份的x軸資料(人均GDP)顯然是不一樣的,所以只能分別繪製之後然後通過overlap層疊在一起~

scatter = (Scatter()
           .add_xaxis([i[0] for i in data[0]])
           .add_yaxis("1990年", [[i[1]] for i in data[0]])
           )
scatter.render_notebook()


座標軸配置

上一步後資料貌似沒有展示出來,不要著急,接著往下做

  1. 設定座標軸型別為value

  2. 順便設定一個座標軸名稱~

scatter = (Scatter()
           .add_xaxis([i[0] for i in data[0]])
           .add_yaxis("1990年", [[i[1], i[2], i[3]] for i in data[0]])
           .set_global_opts(yaxis_opts=opts.AxisOpts(name='人均壽命', type_="value"),
                            xaxis_opts=opts.AxisOpts(name='人均GDP', type_="value"))
           )
scatter.render_notebook()


提示框配置

這部分與原效果不一樣,Echarts中只顯示了國家名稱,我這邊會通過js形式將資料全部顯示到提示框中~

將滑鼠移到圖形上,我們便能看到各項資料值了~

tool_js = """function (param) {return param.seriesName + ' — ' +param.data[3]+'<br/>' 
            +'人均GDP: '+param.data[0]+' 美元<br/>'
            +'GDP總量: '+param.data[2]+' 美元<br/>'
            +'人均壽命: '+param.data[1]+'歲';}"""
            
            
scatter = (Scatter()
           .add_xaxis([i[0] for i in data[0]])
           .add_yaxis("1990年", [[i[1], i[2], i[3]] for i in data[0]])
           .set_global_opts(yaxis_opts=opts.AxisOpts(name='人均壽命', type_="value"),
                            xaxis_opts=opts.AxisOpts(name='人均GDP', type_="value"),
                            tooltip_opts = opts.TooltipOpts(formatter=JsCode(tool_js)))
           )
scatter.render_notebook()

資料項標籤配置

散點多的時候標籤會很亂,這一步關閉標籤顯示~

tool_js = """function (param) {return param.seriesName + ' — ' +param.data[3]+'<br/>' 
            +'人均GDP: '+param.data[0]+' 美元<br/>'
            +'GDP總量: '+param.data[2]+' 美元<br/>'
            +'人均壽命: '+param.data[1]+'歲';}"""
            
            
scatter = (Scatter()
           .add_xaxis([i[0] for i in data[0]])
           .add_yaxis("1990年", [[i[1], i[2], i[3]] for i in data[0]], 
                       # 關閉標籤顯示
                       label_opts=opts.LabelOpts(is_show=False))
           .set_global_opts(yaxis_opts=opts.AxisOpts(name='人均壽命', type_="value"),
                            xaxis_opts=opts.AxisOpts(name='人均GDP', type_="value"),
                            tooltip_opts = opts.TooltipOpts(formatter=JsCode(tool_js)))
           )
scatter.render_notebook()

圖形顏色配置

按照Echarts中顏色的配置,設定徑向漸變配色~

tool_js = """function (param) {return param.seriesName + ' — ' +param.data[3]+'<br/>' 
            +'人均GDP: '+param.data[0]+' 美元<br/>'
            +'GDP總量: '+param.data[2]+' 美元<br/>'
            +'人均壽命: '+param.data[1]+'歲';}"""
            
            
# 配色方案直接從Echarts投過來就好 
item_color_js = """new echarts.graphic.RadialGradient(0.4, 0.3, 1, [{
                                        offset: 0,
                                        color: 'rgb(251, 118, 123)'
                                    }, {
                                        offset: 1,
                                        color: 'rgb(204, 46, 72)'
                                    }])"""           
            
scatter = (Scatter()
           .add_xaxis([i[0] for i in data[0]])
           .add_yaxis("1990年", [[i[1], i[3], i[2]] for i in data[0]], 
                       label_opts=opts.LabelOpts(is_show=False),
                       # 設定圖形顏色
                       itemstyle_opts=opts.ItemStyleOpts(color=JsCode(item_color_js)))
           .set_global_opts(yaxis_opts=opts.AxisOpts(name='人均壽命', type_="value"),
                            xaxis_opts=opts.AxisOpts(name='人均GDP', type_="value"),
                            tooltip_opts = opts.TooltipOpts(formatter=JsCode(tool_js)))
           )
scatter.render_notebook()

畫另一個散點圖

畫另一個散點圖展示2015年的資料,除了圖形顏色不一樣,其他配置均與上一個散點圖一致~

tool_js = """function (param) {return param.seriesName + ' — ' +param.data[3]+'<br/>' 
            +'人均GDP: '+param.data[0]+' 美元<br/>'
            +'GDP總量: '+param.data[2]+' 美元<br/>'
            +'人均壽命: '+param.data[1]+'歲';}"""
            
            

item_color_js = """new echarts.graphic.RadialGradient(0.4, 0.3, 1, [{
                                        offset: 0,
                                        color: 'rgb(129, 227, 238)'
                                    }, {
                                        offset: 1,
                                        color: 'rgb(25, 183, 207)'
                                    }])"""         
            
scatter = (Scatter()
           .add_xaxis([i[0] for i in data[1]])
           .add_yaxis("2015年", [[i[1], i[3], i[2]] for i in data[1]], 
                       label_opts=opts.LabelOpts(is_show=False),
                       itemstyle_opts=opts.ItemStyleOpts(color=JsCode(item_color_js)))
           .set_global_opts(yaxis_opts=opts.AxisOpts(name='人均壽命', type_="value"),
                            xaxis_opts=opts.AxisOpts(name='人均GDP', type_="value"),
                            tooltip_opts = opts.TooltipOpts(formatter=JsCode(tool_js)))
           )
scatter.render_notebook()

多圖層疊

將上面畫好的兩個圖層疊在一起,是不是有點模樣了~

因為兩個圖是共用全域性配置的,所以只需要保留一個圖的全域性配置項就可以了~

tool_js = """function (param) {return param.seriesName + ' — ' +param.data[3]+'<br/>' 
            +'人均GDP: '+param.data[0]+' 美元<br/>'
            +'GDP總量: '+param.data[2]+' 美元<br/>'
            +'人均壽命: '+param.data[1]+'歲';}"""
            

item_color_js_1 = """new echarts.graphic.RadialGradient(0.4, 0.3, 1, [{
                                        offset: 0,
                                        color: 'rgb(251, 118, 123)'
                                    }, {
                                        offset: 1,
                                        color: 'rgb(204, 46, 72)'
                                    }])"""           

item_color_js_2 = """new echarts.graphic.RadialGradient(0.4, 0.3, 1, [{
                                        offset: 0,
                                        color: 'rgb(129, 227, 238)'
                                    }, {
                                        offset: 1,
                                        color: 'rgb(25, 183, 207)'
                                    }])"""  

            
scatter1 = (Scatter()
           .add_xaxis([i[0] for i in data[0]])
           .add_yaxis("1990年", [[i[1], i[3], i[2]] for i in data[0]], 
                       label_opts=opts.LabelOpts(is_show=False),
                       itemstyle_opts=opts.ItemStyleOpts(color=JsCode(item_color_js_1)))
           .set_global_opts(yaxis_opts=opts.AxisOpts(name='人均壽命', type_="value"),
                            xaxis_opts=opts.AxisOpts(name='人均GDP', type_="value"),
                            tooltip_opts = opts.TooltipOpts(formatter=JsCode(tool_js)))
           )
           
           
scatter2 = (Scatter()
           .add_xaxis([i[0] for i in data[1]])
           .add_yaxis("2015年", [[i[1], i[3], i[2]] for i in data[1]], 
                       label_opts=opts.LabelOpts(is_show=False),
                       itemstyle_opts=opts.ItemStyleOpts(color=JsCode(item_color_js_2)))
           )


scatter1.overlap(scatter2)           
scatter1.render_notebook()

設定Y軸起始點

預設座標軸起始都是0,但這樣會讓所有的圖形都擠到一塊了不好區分,所以這邊將Y軸的起始位置修改一下~

tool_js = """function (param) {return param.seriesName + ' — ' +param.data[3]+'<br/>' 
            +'人均GDP: '+param.data[0]+' 美元<br/>'
            +'GDP總量: '+param.data[2]+' 美元<br/>'
            +'人均壽命: '+param.data[1]+'歲';}"""


item_color_js_1 = """new echarts.graphic.RadialGradient(0.4, 0.3, 1, [{
                offset: 0,
                color: 'rgb(251, 118, 123)'
            }, {
                offset: 1,
                color: 'rgb(204, 46, 72)'
            }])"""

item_color_js_2 = """new echarts.graphic.RadialGradient(0.4, 0.3, 1, [{
                offset: 0,
                color: 'rgb(129, 227, 238)'
            }, {
                offset: 1,
                color: 'rgb(25, 183, 207)'
            }])"""




scatter1 = (Scatter()
            .add_xaxis([i[0] for i in data[0]])
            .add_yaxis("1990年", [[i[1], i[2], i[3]] for i in data[0]],
                       label_opts=opts.LabelOpts(is_show=False),
                       itemstyle_opts=opts.ItemStyleOpts(color0='rgba(25, 100, 150, 0.5)', color=JsCode(item_color_js_1)))
            .set_global_opts(yaxis_opts=opts.AxisOpts(name='人均壽命', type_="value", 
                                                      # 預設為False,即起始為0  
                                                      is_scale=True),
                             xaxis_opts=opts.AxisOpts(
                name='人均GDP', type_="value"),
    tooltip_opts=opts.TooltipOpts(formatter=JsCode(tool_js)))
)


scatter2 = (Scatter()
            .add_xaxis([i[0] for i in data[1]])
            .add_yaxis("2015年", [[i[1], i[2], i[3]] for i in data[1]],
                       label_opts=opts.LabelOpts(is_show=False),
                       itemstyle_opts=opts.ItemStyleOpts(color=JsCode(item_color_js_2)))
            )


scatter1.overlap(scatter2)
scatter1.render_notebook()

圖形大小配置

到這一步其實我們一直沒有用到GDP總量資料,這一步將GDP總量的資料對映到圖形大小;

這裡需要注意一下,正常情況下我們通過視覺元件去配置就完全OK的,但這裡為了與原始效果一樣,咱們採取通過執行JS函式來設定圖形大小~

tool_js = """function (param) {return param.seriesName + ' — ' +param.data[3]+'<br/>' 
            +'人均GDP: '+param.data[0]+' 美元<br/>'
            +'GDP總量: '+param.data[2]+' 美元<br/>'
            +'人均壽命: '+param.data[1]+'歲';}"""


item_color_js_1 = """new echarts.graphic.RadialGradient(0.4, 0.3, 1, [{
                offset: 0,
                color: 'rgb(251, 118, 123)'
            }, {
                offset: 1,
                color: 'rgb(204, 46, 72)'
            }])"""

item_color_js_2 = """new echarts.graphic.RadialGradient(0.4, 0.3, 1, [{
                offset: 0,
                color: 'rgb(129, 227, 238)'
            }, {
                offset: 1,
                color: 'rgb(25, 183, 207)'
            }])"""

# 這個函式會根據GDP總量的資料計算一個數值,用於配置圖形大小
symbol_js = """function (data) {return Math.sqrt(data[2]) / 5e2;}"""



scatter1 = (Scatter()
            .add_xaxis([i[0] for i in data[0]])
            .add_yaxis("1990年", [[i[1], i[2], i[3]] for i in data[0]],
                       # 這裡配置圖形大小,根據GDP總量計算出symbol_size
                       symbol_size=JsCode(symbol_js),
                       label_opts=opts.LabelOpts(is_show=False),
                       itemstyle_opts=opts.ItemStyleOpts(color0='rgba(25, 100, 150, 0.5)', color=JsCode(item_color_js_1)))
            .set_global_opts(yaxis_opts=opts.AxisOpts(name='人均壽命', type_="value", is_scale=True),
                             xaxis_opts=opts.AxisOpts(
                name='人均GDP', type_="value"),
    tooltip_opts=opts.TooltipOpts(formatter=JsCode(tool_js)))
)


scatter2 = (Scatter()
            .add_xaxis([i[0] for i in data[1]])
            .add_yaxis("2015年", [[i[1], i[2], i[3]] for i in data[1]],
                       # 這裡配置圖形大小,根據GDP總量計算出symbol_size
                       symbol_size=JsCode(symbol_js),
                       label_opts=opts.LabelOpts(is_show=False),
                       itemstyle_opts=opts.ItemStyleOpts(color=JsCode(item_color_js_2)))
            )


scatter1.overlap(scatter2)
scatter1.render_notebook()

圖形陰影效果

在pyecharts中ItemStyleOpts其實是沒包含陰陽引數配置的,不過對於Pyecharts中的引數其實都支援直接傳入如下字典形式來配置的。

item_style = {  
    'shadowBlur': 10,  
    'shadowColor': 'rgba(120, 36, 50, 0.5)',  
    'shadowOffsetY': 5,  
    'color': JsCode(item_color_js_1)  
}
tool_js = """function (param) {return param.seriesName + ' — ' +param.data[3]+'<br/>' 
            +'人均GDP: '+param.data[0]+' 美元<br/>'
            +'GDP總量: '+param.data[2]+' 美元<br/>'
            +'人均壽命: '+param.data[1]+'歲';}"""

item_color_js_1 = """new echarts.graphic.RadialGradient(0.4, 0.3, 1, [{
                offset: 0,
                color: 'rgb(251, 118, 123)'
            }, {
                offset: 1,
                color: 'rgb(204, 46, 72)'
            }])"""

item_color_js_2 = """new echarts.graphic.RadialGradient(0.4, 0.3, 1, [{
                offset: 0,
                color: 'rgb(129, 227, 238)'
            }, {
                offset: 1,
                color: 'rgb(25, 183, 207)'
            }])"""

symbol_js = """function (data) {return Math.sqrt(data[2]) / 5e2;}"""

# 圖元樣式配置,通過字典傳入,包含陰影的設定
item_style_1 = {
    'shadowBlur': 10,
    'shadowColor': 'rgba(120, 36, 50, 0.5)',
    'shadowOffsetY': 5,
    'color': JsCode(item_color_js_1)
}

# 圖元樣式配置,通過字典傳入
item_style_2 = {
    'shadowBlur': 10,
    'shadowColor': 'rgba(120, 36, 50, 0.5)',
    'shadowOffsetY': 5,
    'color': JsCode(item_color_js_2)
}


scatter1 = (Scatter()
            .add_xaxis([i[0] for i in data[0]])
            .add_yaxis("1990年", [[i[1], i[2], i[3]] for i in data[0]],
                       symbol_size=JsCode(symbol_js),
                       label_opts=opts.LabelOpts(is_show=False),
                       # 這裡傳入
                       itemstyle_opts=item_style_1)
            .set_global_opts(yaxis_opts=opts.AxisOpts(name='人均壽命', type_="value", is_scale=True),
                             xaxis_opts=opts.AxisOpts(
                name='人均GDP', type_="value"),
    tooltip_opts=opts.TooltipOpts(formatter=JsCode(tool_js)))
)


scatter2 = (Scatter()
            .add_xaxis([i[0] for i in data[1]])
            .add_yaxis("2015年", [[i[1], i[2], i[3]] for i in data[1]],
                       symbol_size=JsCode(symbol_js),
                       label_opts=opts.LabelOpts(is_show=False),
                       # 這裡傳入
                       itemstyle_opts=item_style_2)
            )


scatter1.overlap(scatter2)
scatter1.render_notebook()


圖形背景顏色配置
tool_js = """function (param) {return param.seriesName + ' — ' +param.data[3]+'<br/>' 
            +'人均GDP: '+param.data[0]+' 美元<br/>'
            +'GDP總量: '+param.data[2]+' 美元<br/>'
            +'人均壽命: '+param.data[1]+'歲';}"""


item_color_js_1 = """new echarts.graphic.RadialGradient(0.4, 0.3, 1, [{
                offset: 0,
                color: 'rgb(251, 118, 123)'
            }, {
                offset: 1,
                color: 'rgb(204, 46, 72)'
            }])"""

item_color_js_2 = """new echarts.graphic.RadialGradient(0.4, 0.3, 1, [{
                offset: 0,
                color: 'rgb(129, 227, 238)'
            }, {
                offset: 1,
                color: 'rgb(25, 183, 207)'
            }])"""

symbol_js = """function (data) {return Math.sqrt(data[2]) / 5e2;}"""

item_style_1 = {
    'shadowBlur': 10,
    'shadowColor': 'rgba(120, 36, 50, 0.5)',
    'shadowOffsetY': 5,
    'color': JsCode(item_color_js_1)
}

item_style_2 = {
    'shadowBlur': 10,
    'shadowColor': 'rgba(120, 36, 50, 0.5)',
    'shadowOffsetY': 5,
    'color': JsCode(item_color_js_2)
}

# 直接偷echarts的配色方案
bg_color_js = """
new echarts.graphic.RadialGradient(0.3, 0.3, 0.8, [{
        offset: 0,
        color: '#f7f8fa'
    }, {
        offset: 1,
        color: '#cdd0d5'
    }])"""

scatter1 = (Scatter(init_opts=opts.InitOpts(bg_color=JsCode(bg_color_js)))
            .add_xaxis([i[0] for i in data[0]])
            .add_yaxis("1990年", [[i[1], i[2], i[3]] for i in data[0]],
                       symbol_size=JsCode(symbol_js),
                       label_opts=opts.LabelOpts(is_show=False),
                       itemstyle_opts=item_style_1)
            .set_global_opts(yaxis_opts=opts.AxisOpts(name='人均壽命', type_="value", is_scale=True),
                             xaxis_opts=opts.AxisOpts(
                name='人均GDP', type_="value"),
    tooltip_opts=opts.TooltipOpts(formatter=JsCode(tool_js)))
)


scatter2 = (Scatter()
            .add_xaxis([i[0] for i in data[1]])
            .add_yaxis("2015年", [[i[1], i[2], i[3]] for i in data[1]],
                       symbol_size=JsCode(symbol_js),
                       label_opts=opts.LabelOpts(is_show=False),
                       itemstyle_opts=item_style_2)
            )


scatter1.overlap(scatter2)
scatter1.render_notebook()


圖形長/寬設定
tool_js = """function (param) {return param.seriesName + ' — ' +param.data[3]+'<br/>' 
            +'人均GDP: '+param.data[0]+' 美元<br/>'
            +'GDP總量: '+param.data[2]+' 美元<br/>'
            +'人均壽命: '+param.data[1]+'歲';}"""


item_color_js_1 = """new echarts.graphic.RadialGradient(0.4, 0.3, 1, [{
                offset: 0,
                color: 'rgb(251, 118, 123)'
            }, {
                offset: 1,
                color: 'rgb(204, 46, 72)'
            }])"""

item_color_js_2 = """new echarts.graphic.RadialGradient(0.4, 0.3, 1, [{
                offset: 0,
                color: 'rgb(129, 227, 238)'
            }, {
                offset: 1,
                color: 'rgb(25, 183, 207)'
            }])"""

symbol_js = """function (data) {return Math.sqrt(data[2]) / 5e2;}"""

item_style_1 = {
    'shadowBlur': 10,
    'shadowColor': 'rgba(120, 36, 50, 0.5)',
    'shadowOffsetY': 5,
    'color': JsCode(item_color_js_1)
}

item_style_2 = {
    'shadowBlur': 10,
    'shadowColor': 'rgba(120, 36, 50, 0.5)',
    'shadowOffsetY': 5,
    'color': JsCode(item_color_js_2)
}

bg_color_js = """
new echarts.graphic.RadialGradient(0.3, 0.3, 0.8, [{
        offset: 0,
        color: '#f7f8fa'
    }, {
        offset: 1,
        color: '#cdd0d5'
    }])"""

scatter1 = (Scatter(init_opts=opts.InitOpts(bg_color=JsCode(bg_color_js),
                                            # 長寬設定
                                            width='1000px', height='800px'))
            .add_xaxis([i[0] for i in data[0]])
            .add_yaxis("1990年", [[i[1], i[2], i[3]] for i in data[0]],
                       symbol_size=JsCode(symbol_js),
                       label_opts=opts.LabelOpts(is_show=False),
                       itemstyle_opts=item_style_1)
            .set_global_opts(yaxis_opts=opts.AxisOpts(name='人均壽命', type_="value", is_scale=True),
                             xaxis_opts=opts.AxisOpts(name='人均GDP', type_="value"),
                            tooltip_opts=opts.TooltipOpts(formatter=JsCode(tool_js)),
                            legend_opts=opts.LegendOpts(is_show=True, pos_right=10))
)


scatter2 = (Scatter()
            .add_xaxis([i[0] for i in data[1]])
            .add_yaxis("2015年", [[i[1], i[2], i[3]] for i in data[1]],
                       symbol_size=JsCode(symbol_js),
                       label_opts=opts.LabelOpts(is_show=False),
                       itemstyle_opts=item_style_2)
            )


scatter1.overlap(scatter2)
scatter1.render_notebook()

新增分割線

這裡注意線性配置裡設定為dashed~

tool_js = """function (param) {return param.seriesName + ' — ' +param.data[3]+'<br/>' 
            +'人均GDP: '+param.data[0]+' 美元<br/>'
            +'GDP總量: '+param.data[2]+' 美元<br/>'
            +'人均壽命: '+param.data[1]+'歲';}"""


item_color_js_1 = """new echarts.graphic.RadialGradient(0.4, 0.3, 1, [{
                offset: 0,
                color: 'rgb(251, 118, 123)'
            }, {
                offset: 1,
                color: 'rgb(204, 46, 72)'
            }])"""

item_color_js_2 = """new echarts.graphic.RadialGradient(0.4, 0.3, 1, [{
                offset: 0,
                color: 'rgb(129, 227, 238)'
            }, {
                offset: 1,
                color: 'rgb(25, 183, 207)'
            }])"""

symbol_js = """function (data) {return Math.sqrt(data[2]) / 5e2;}"""

item_style_1 = {
    'shadowBlur': 10,
    'shadowColor': 'rgba(120, 36, 50, 0.5)',
    'shadowOffsetY': 5,
    'color': JsCode(item_color_js_1)
}

item_style_2 = {
    'shadowBlur': 10,
    'shadowColor': 'rgba(120, 36, 50, 0.5)',
    'shadowOffsetY': 5,
    'color': JsCode(item_color_js_2)
}

bg_color_js = """
new echarts.graphic.RadialGradient(0.3, 0.3, 0.8, [{
        offset: 0,
        color: '#f7f8fa'
    }, {
        offset: 1,
        color: '#cdd0d5'
    }])"""

scatter1 = (Scatter(init_opts=opts.InitOpts(bg_color=JsCode(bg_color_js),width='1000px', height='800px'))
            .add_xaxis([i[0] for i in data[0]])
            .add_yaxis("1990年", [[i[1], i[2], i[3]] for i in data[0]],
                       symbol_size=JsCode(symbol_js),
                       label_opts=opts.LabelOpts(is_show=False),
                       itemstyle_opts=item_style_1)
            .set_global_opts(yaxis_opts=opts.AxisOpts(name='人均壽命', type_="value", is_scale=True,
                                                     splitline_opts=opts.SplitLineOpts(is_show=True, linestyle_opts=opts.LineStyleOpts(type_='dashed'))),
                             xaxis_opts=opts.AxisOpts(name='人均GDP', type_="value", 
                                                     splitline_opts=opts.SplitLineOpts(is_show=True, linestyle_opts=opts.LineStyleOpts(type_='dashed'))),
                             tooltip_opts=opts.TooltipOpts(formatter=JsCode(tool_js)))
            )


scatter2 = (Scatter()
            .add_xaxis([i[0] for i in data[1]])
            .add_yaxis("2015年", [[i[1], i[2], i[3]] for i in data[1]],
                       symbol_size=JsCode(symbol_js),
                       label_opts=opts.LabelOpts(is_show=False),
                       itemstyle_opts=item_style_2)
            )


scatter1.overlap(scatter2)
scatter1.render_notebook()



新增標題,完工!

新增標題,順便將圖例位置調到右邊,完工!!!

tool_js = """function (param) {return param.seriesName + ' — ' +param.data[3]+'<br/>' 
            +'人均GDP: '+param.data[0]+' 美元<br/>'
            +'GDP總量: '+param.data[2]+' 美元<br/>'
            +'人均壽命: '+param.data[1]+'歲';}"""


item_color_js_1 = """new echarts.graphic.RadialGradient(0.4, 0.3, 1, [{
                offset: 0,
                color: 'rgb(251, 118, 123)'
            }, {
                offset: 1,
                color: 'rgb(204, 46, 72)'
            }])"""

item_color_js_2 = """new echarts.graphic.RadialGradient(0.4, 0.3, 1, [{
                offset: 0,
                color: 'rgb(129, 227, 238)'
            }, {
                offset: 1,
                color: 'rgb(25, 183, 207)'
            }])"""

symbol_js = """function (data) {return Math.sqrt(data[2]) / 5e2;}"""

item_style_1 = {
    'shadowBlur': 10,
    'shadowColor': 'rgba(120, 36, 50, 0.5)',
    'shadowOffsetY': 5,
    'color': JsCode(item_color_js_1)
}

item_style_2 = {
    'shadowBlur': 10,
    'shadowColor': 'rgba(120, 36, 50, 0.5)',
    'shadowOffsetY': 5,
    'color': JsCode(item_color_js_2)
}

bg_color_js = """
new echarts.graphic.RadialGradient(0.3, 0.3, 0.8, [{
        offset: 0,
        color: '#f7f8fa'
    }, {
        offset: 1,
        color: '#cdd0d5'
    }])"""

scatter1 = (Scatter(init_opts=opts.InitOpts(bg_color=JsCode(bg_color_js),width='1000px', height='800px'))
            .add_xaxis([i[0] for i in data[0]])
            .add_yaxis("1990年", [[i[1], i[2], i[3]] for i in data[0]],
                       symbol_size=JsCode(symbol_js),
                       label_opts=opts.LabelOpts(is_show=False),
                       itemstyle_opts=item_style_1)
            .set_global_opts(yaxis_opts=opts.AxisOpts(name='人均壽命', type_="value", is_scale=True,
                                                     splitline_opts=opts.SplitLineOpts(is_show=True, linestyle_opts=opts.LineStyleOpts(type_='dashed'))),
                             xaxis_opts=opts.AxisOpts(name='人均GDP', type_="value", 
                                                     splitline_opts=opts.SplitLineOpts(is_show=True, linestyle_opts=opts.LineStyleOpts(type_='dashed'))),
                             tooltip_opts=opts.TooltipOpts(formatter=JsCode(tool_js)),
                             legend_opts=opts.LegendOpts(is_show=True, pos_right=10),
                             title_opts=opts.TitleOpts(title="1990 與 2015 年各國家人均壽命與 GDP"))
            )


scatter2 = (Scatter()
            .add_xaxis([i[0] for i in data[1]])
            .add_yaxis("2015年", [[i[1], i[2], i[3]] for i in data[1]],
                       symbol_size=JsCode(symbol_js),
                       label_opts=opts.LabelOpts(is_show=False),
                       itemstyle_opts=item_style_2)
            )


scatter1.overlap(scatter2)
scatter1.render_notebook()

天道酬勤 循序漸進 技壓群雄