14.小结-----近年各国GDP比较

近年来中国及各国GDP变化

一.可视化数据来源

经各方面搜索,最终选择此数据来源
https://www.kylc.com/stats/global/yearly_per_country/g_gdp/chn.html
整理后数据如下

中国GDP 6.09, 7.55, 8.53, 9.57, 10.48, 11.06, 11.23, 12.31, 13.89, 14.28 
美国GDP 14.99, 15.544, 16.2, 16.78, 17.53, 18.22, 18.71, 19.52, 20.58, 21.43
日本GDP 5.7, 6.16, 6.2, 5.16, 4.85, 4.39, 4.92, 4.87, 4.95, 5.08
中国GDP在世界占比 9.2, 10.2, 11.3, 12.3, 13.1, 14.7, 14.7, 15.1, 16.0, 16.2
2019年各国GDP占比  中国 16.2, 韩国1.8, 印度3.2, 德国4.4, 日本5.7, 美国24.4,其他44.3
                   对应关系  "中国", "韩国", "印度", "德国", "日本", "其他国家"
                   			16.2, 1.8, 3.2, 4.4, 5.7, 44.3

二.代码及展示

(1.)生成json的代码如下

import pyecharts.options as opts
from pyecharts.charts import Pie,Bar, Grid, Line,Liquid,Page
from pyecharts.globals import SymbolType
#------------------------------------------------------------------------------------------------------------------
#------------------------------------------------------------------------------------------------------------------
##标题------------------------------------------------------------------------------------------------------------------
title=(Pie(init_opts=opts.InitOpts(bg_color="black"))

.set_global_opts(
    title_opts=opts.TitleOpts(title="中国近年人口数量,出生率自然增长率",                        
                                pos_top="middle",
                                pos_left="center",
                                title_textstyle_opts=opts.TextStyleOpts(color="red", font_size=40),))
      )

#------------------------------------------------------------------------------------------------------------------
#------------------------------------------------------------------------------------------------------------------
##饼图------------------------------------------------------------------------------------------------------------------
x_data = ["中国", "韩国", "印度", "德国", "日本", "其他国家"]
y_data = [16.2, 1.8, 3.2, 4.4, 5.7, 44.3]
a=Pie(init_opts=opts.InitOpts(width="1000px", height="900px"),)
a.add(
     series_name="访问来源",
     data_pair=[list(z) for z in zip(x_data, y_data)],
     radius=["30%", "60%"],
    # label_opts=opts.LabelOpts(is_show=False, position="center"),
 )
a.set_global_opts(title_opts=opts.TitleOpts(title="中国及世界各国GDP占比")
                  #,legend_opts=opts.LegendOpts(pos_left="legft", orient="vertical")#使标签竖着排列
                 )
a.set_series_opts(
     tooltip_opts=opts.TooltipOpts(
         trigger="item", formatter="{a} <br/>{b}: {c} ({d}%)"
     ),
      label_opts=opts.LabelOpts(formatter="{b}: {c}") #引导线及标签
 )
#a.render_notebook()

#------------------------------------------------------------------------------------------------------------------
#------------------------------------------------------------------------------------------------------------------
##水仙图------------------------------------------------------------------------------------------------------------------


c = (
    Liquid()
    .add("2019中国在世界占比", [0.16, 0.83], is_outline_show=False, shape=SymbolType.DIAMOND)
    .set_global_opts(title_opts=opts.TitleOpts(title="2019中国在世界占比"))
    #.render("水仙图.html")
)

#------------------------------------------------------------------------------------------------------------------
#------------------------------------------------------------------------------------------------------------------
##组合图------------------------------------------------------------------------------------------------------------------


bar = (
    Bar()
    .add_xaxis(["{}年".format(i) for i in range(2010, 2020)])
    .add_yaxis(
        "中国GDP",
        [6.09, 7.55, 8.53, 9.57, 10.48, 11.06, 11.23, 12.31, 13.89, 14.28],
        yaxis_index=0,
        color="#d14a61",
    )
    .add_yaxis(
        "美国GDP",
        [14.99, 15.544, 16.2, 16.78, 17.53, 18.22, 18.71, 19.52, 20.58, 21.43],
        yaxis_index=1,
        color="#5793f3",
    )
    .extend_axis(
        yaxis=opts.AxisOpts(
            name="中国GDP",
            type_="value",
            min_=0,
            max_=25,
            position="right",
            axisline_opts=opts.AxisLineOpts(
                linestyle_opts=opts.LineStyleOpts(color="#d14a61")
            ),
            axislabel_opts=opts.LabelOpts(formatter="{value} 万亿美元"),
        )
    )
    .extend_axis(
        yaxis=opts.AxisOpts(
            type_="value",
            name="占比",
            min_=0,
            max_=20,
            position="left",
            axisline_opts=opts.AxisLineOpts(
                linestyle_opts=opts.LineStyleOpts(color="#675bba")
            ),
            axislabel_opts=opts.LabelOpts(formatter="{value} %"),
            splitline_opts=opts.SplitLineOpts(
                is_show=True, linestyle_opts=opts.LineStyleOpts(opacity=1)
            ),
        )
    )
    .set_global_opts(
        yaxis_opts=opts.AxisOpts(
            name="美国GDP",
            min_=0,
            max_=25,
            position="right",
            offset=80,
            axisline_opts=opts.AxisLineOpts(
                linestyle_opts=opts.LineStyleOpts(color="#5793f3")
            ),
            axislabel_opts=opts.LabelOpts(formatter="{value} 万亿美元"),
        ),
        title_opts=opts.TitleOpts(title="中国与美国、日本GDP比较"),
        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(2010, 2020)])
    .add_yaxis(
        "中国占比",
        [9.2, 10.2, 11.3, 12.3, 13.1, 14.7, 14.7, 15.1, 16.0, 16.2],
        yaxis_index=2,
        color="#675bba",
        label_opts=opts.LabelOpts(is_show=False),
    )
)

bar1 = (
    Bar()
    .add_xaxis(["{}年".format(i) for i in range(2010, 2020)])
    .add_yaxis(
        "中国GDP 1",
        [6.09, 7.55, 8.53, 9.57, 10.48, 11.06, 11.23, 12.31, 13.89, 14.28],
        color="#d14a61",
        xaxis_index=1,
        yaxis_index=3,
    )
    .add_yaxis(
        "日本GDP",
        [5.7, 6.16, 6.2, 5.16, 4.85, 4.39, 4.92, 4.87, 4.95, 5.08],
        color="#5793f3",
        xaxis_index=1,
        yaxis_index=3,
    )
    .extend_axis(
        yaxis=opts.AxisOpts(
            name="中国GDP",
            type_="value",
            min_=0,
            max_=16,
            position="right",
            axisline_opts=opts.AxisLineOpts(
                linestyle_opts=opts.LineStyleOpts(color="#d14a61")
            ),
            axislabel_opts=opts.LabelOpts(formatter="{value} 万亿美元"),
        )
    )
    .extend_axis(
        yaxis=opts.AxisOpts(
            type_="value",
            name="占比",
            min_=0,
            max_=20,
            position="left",
            axisline_opts=opts.AxisLineOpts(
                linestyle_opts=opts.LineStyleOpts(color="#675bba")
            ),
            axislabel_opts=opts.LabelOpts(formatter="{value} %"),
            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="日本GDP",
            min_=0,
            max_=15,
            position="right",
            offset=80,
            grid_index=1,
            axisline_opts=opts.AxisLineOpts(
                linestyle_opts=opts.LineStyleOpts(color="#5793f3")
            ),
            axislabel_opts=opts.LabelOpts(formatter="{value} 万亿美元"),
        ),
        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(2010, 2020)])
    .add_yaxis(
        "中国占比",
        [9.2, 10.2, 11.3, 12.3, 13.1, 14.7, 14.7, 15.1, 16.0, 16.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("组合图.html")
)

#------------------------------------------------------------------------------------------------------------------
#------------------------------------------------------------------------------------------------------------------
##网页------------------------------------------------------------------------------------------------------------------
page=Page(layout=Page.DraggablePageLayout)
page.add(title,a,c,grid,)
page.render("result.html")
#如果不想保存在默认地址中,可以直接写入想保存的地址,但是文件级别直接不可以写“\”,而应该写"\\",列如
“C:\\Users\\lenovo\\Desktop\\可视化作业\\result.html”

(2.)将生成的网页打开,结果如下,是一张张自上而下排列的图,并不是理想结果
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

(3.)调整后结果如下
在这里插入图片描述
(4.)保存调整后的json
点击左上角的save
在这里插入图片描述

(5.)json转html【重新渲染html】

page.save_resize_html("result.html",cfg_file="C:\\Users\\lenovo\\Desktop\\可视化作业\\chart_config.json",dest="C:\\Users\\lenovo\\Desktop\\可视化作业\\re_result.html")
#cfg_file 后加  生成的json文件地址  
#dest 后加  最终生成的网页存放的地址
#json转html---重新渲染html

(6.)此时将生成的网页打开,结果如下
请添加图片描述
(7.)为什么要保存json重新渲染
如果不这样做,刷新网页后又会恢复调整前的状态,重新渲染后,再次生成的网页打开即为想要的效果

三.可视化分析

根据作图结果分析,中国GDP日益提升,与日本相比较高,但还是没有超出美国,还有提升的空间,随着我国国际地位及经济、政治各方面的变化,我国GDP预测会持续变好!

猜你喜欢

转载自blog.csdn.net/m0_46222433/article/details/119877579