<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          Python做的交互式動(dòng)態(tài)大圖真漂亮

          共 10962字,需瀏覽 22分鐘

           ·

          2020-12-25 02:00

          今天給大家分享1個(gè)pyecharts交互式動(dòng)態(tài)可視化案例,通過先拆分、后組合的方式,一步步教你如何實(shí)現(xiàn),具體成果如下:


          本次案例數(shù)據(jù)來源于國家統(tǒng)計(jì)局,通過爬蟲獲取,這里已給大家備好,請(qǐng)?jiān)谖哪┇@取

          一、繪制基本圖形

          用pandas讀取數(shù)據(jù),通過整合數(shù)據(jù)格式,分別用pyecharts繪制地圖、柱狀圖、餅圖,具體內(nèi)容如下:

          1.繪制地圖

          import?pyecharts.options?as?opts
          from?pyecharts.globals?import?ThemeType
          from?pyecharts.commons.utils?import?JsCode
          from?pyecharts.charts?import?Timeline,?Grid,?Bar,?Map,?Pie
          import?pandas?as?pd
          data?=?pd.read_excel('全國各省財(cái)政收入.xlsx',index_col=0)
          years=list(data.keys())?#獲取列名
          citys=list(data.index)????#獲取索引行名
          citys=[city.replace('省','').replace('市','').replace('自治區(qū)','')?for?city?in?citys]
          datas=[]
          for?y?in?years:
          ????dict_year={}
          ????dict_year['time']=y
          ????data_list=[[i,j]?for?i,j?in?zip(citys,list(data[y]))]
          ????dict_year['data']=sorted(data_list,?key=(lambda?x:?x[1]),reverse=True)
          ????datas.append(dict_year)
          map_data?=?[i["data"]?for?i?in?datas?if?i["time"]==2010][0]
          min_data,?max_data?=?(
          ????????min([d[1]?for?d?in?map_data]),
          ????????max([d[1]?for?d?in?map_data]),
          ????)
          map_chart?=?(
          ????????Map(init_opts=opts.InitOpts(theme=ThemeType.DARK))
          ????????.add(
          ????????????series_name="",
          ????????????data_pair=map_data,
          ????????????label_opts=opts.LabelOpts(is_show=False),
          ????????????is_map_symbol_show=False,
          ????????)
          ????????.set_global_opts(
          ????????????title_opts=opts.TitleOpts(
          ????????????????title="2000年以來中國各省GDP排名變化情況",
          ????????????????subtitle="GDP單位:億元",
          ????????????????pos_left="center",
          ????????????????pos_top="top",
          ????????????????title_textstyle_opts=opts.TextStyleOpts(
          ????????????????????font_size=25,?color="rgba(123,104,238,?0.9)"
          ????????????????),
          ????????????),
          ????????????visualmap_opts=opts.VisualMapOpts(
          ????????????????is_calculable=True,
          ????????????????dimension=0,
          ????????????????pos_left="10",
          ????????????????pos_top="center",
          ????????????????range_text=["High",?"Low"],
          ????????????????range_color=["lightskyblue",?"yellow",?"orangered"],
          ????????????????textstyle_opts=opts.TextStyleOpts(color="#ddd"),
          ????????????????min_=min_data,
          ????????????????max_=max_data,
          ????????????),
          ????????)
          ????)
          map_chart.render_notebook()

          2.繪制柱狀圖


          map_data?=?[i["data"]?for?i?in?datas?if?i["time"]==y][0]
          min_data,?max_data?=?(
          ????min([d[1]?for?d?in?map_data]),
          ????max([d[1]?for?d?in?map_data]),
          )
          bar_x_data?=?[x[0]?for?x?in?map_data]
          bar_y_data?=?[x[1]?for?x?in?map_data]
          bar?=?(
          ????????Bar(init_opts=opts.InitOpts(theme=ThemeType.DARK))
          ????????.add_xaxis(xaxis_data=bar_x_data)
          ????????.add_yaxis(
          ????????????series_name="",
          ????????????yaxis_data=bar_y_data,
          ????????????label_opts=opts.LabelOpts(
          ????????????????is_show=True,?position="right",?formatter=":?{c}"
          ????????????),
          ????????)
          ????????.reversal_axis()
          ????????.set_global_opts(
          ????????????title_opts=opts.TitleOpts(
          ????????????????title="2000年以來中國各省GDP排名變化情況",
          ????????????????subtitle="GDP單位:億元",
          ????????????????pos_left="center",
          ????????????????pos_top="top",
          ????????????????title_textstyle_opts=opts.TextStyleOpts(
          ????????????????????font_size=25,?color="rgba(123,104,238,?0.9)"
          ????????????????),
          ????????????),
          ????????????xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(is_show=False)),
          ????????????yaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(is_show=False)),
          ????????????tooltip_opts=opts.TooltipOpts(is_show=False),
          ????????????visualmap_opts=opts.VisualMapOpts(
          ????????????????is_calculable=True,
          ????????????????dimension=0,
          ????????????????pos_left="10",
          ????????????????pos_top="center",
          ????????????????range_text=["High",?"Low"],
          ????????????????range_color=["lightskyblue",?"yellow",?"orangered"],
          ????????????????textstyle_opts=opts.TextStyleOpts(color="#ddd"),
          ????????????????min_=min_data,
          ????????????????max_=max_data,
          ????????????),
          ????????)
          ????)
          bar.render_notebook()

          3.繪制餅圖

          pie_data?=?[[x[0],?x[1]]?for?x?in?map_data]
          percent_sum?=?sum([x[1]?for?x?in?map_data])
          rest_value?=?0
          for?d?in?map_data:
          ????rest_percent?=?100.0
          ????rest_percent?=?rest_percent?-?percent_sum
          ????rest_value?=?d[1]?*?(rest_percent?/?d[1])
          pie?=?(
          ????Pie(init_opts=opts.InitOpts(theme=ThemeType.DARK))
          ????.add(
          ????????series_name="",
          ????????data_pair=pie_data,
          ????????radius=["12%",?"20%"],
          ????????center=["50%",?"50%"],
          ????????itemstyle_opts=opts.ItemStyleOpts(
          ????????????border_width=1,?border_color="rgba(0,0,0,0.3)"
          ????????),
          ????)
          ????.set_global_opts(
          ????????title_opts=opts.TitleOpts(
          ????????????????title="2000年以來中國各省GDP排名變化情況",
          ????????????????subtitle="GDP單位:億元",
          ????????????????pos_left="center",
          ????????????????pos_top="top",
          ????????????????title_textstyle_opts=opts.TextStyleOpts(
          ????????????????????font_size=25,?color="rgba(123,104,238,?0.9)"
          ????????????????),
          ????????????),
          ????????tooltip_opts=opts.TooltipOpts(is_show=True,?formatter="?go7utgvlrp%"),
          ????????legend_opts=opts.LegendOpts(is_show=False),
          ????)
          )
          pie.render_notebook()

          二、繪制動(dòng)圖

          在基本圖形的基礎(chǔ)上,引入timeline函數(shù),繪制相應(yīng)動(dòng)態(tài)圖形:

          1.繪制動(dòng)態(tài)地圖

          def?get_year_chart(year:?int):
          ????map_data?=?[i["data"]?for?i?in?datas?if?i["time"]==year][0]
          ????min_data,?max_data?=?(
          ????????????min([d[1]?for?d?in?map_data]),
          ????????????max([d[1]?for?d?in?map_data]),
          ????????)
          ????map_chart?=?(
          ????????????Map(init_opts=opts.InitOpts(theme=ThemeType.DARK))
          ????????????.add(
          ????????????????series_name="",
          ????????????????data_pair=map_data,
          ????????????????label_opts=opts.LabelOpts(is_show=False),
          ????????????????is_map_symbol_show=False,
          ????????????)
          ????????????.set_global_opts(
          ????????????????title_opts=opts.TitleOpts(
          ????????????????????title="{}年以來中國各省GDP排名情況".format(year),
          ????????????????????subtitle="GDP單位:億元",
          ????????????????????pos_left="center",
          ????????????????????pos_top="top",
          ????????????????????title_textstyle_opts=opts.TextStyleOpts(
          ????????????????????????font_size=25,?color="rgba(123,104,238,?0.9)"
          ????????????????????),
          ????????????????),
          ????????????????visualmap_opts=opts.VisualMapOpts(
          ????????????????????is_calculable=True,
          ????????????????????dimension=0,
          ????????????????????pos_left="10",
          ????????????????????pos_top="center",
          ????????????????????range_text=["High",?"Low"],
          ????????????????????range_color=["lightskyblue",?"yellow",?"orangered"],
          ????????????????????textstyle_opts=opts.TextStyleOpts(color="#ddd"),
          ????????????????????min_=min_data,
          ????????????????????max_=max_data,
          ????????????????),
          ????????????)
          ????????)
          ????return?map_chart
          time_list?=?list(range(2000,2020))
          timeline?=?Timeline(
          ????init_opts=opts.InitOpts(width="1000px",?height="800px",?theme=ThemeType.DARK)
          )
          for?y?in?time_list:
          ????g?=?get_year_chart(year=y)
          ????timeline.add(g,?time_point=str(y))

          timeline.add_schema(
          ????orient="vertical",
          ????is_auto_play=True,
          ????is_inverse=True,
          ????play_interval=500,
          ????pos_left="null",
          ????pos_right="5",
          ????pos_top="20",
          ????pos_bottom="20",
          ????width="50",
          ????label_opts=opts.LabelOpts(is_show=True,?color="#fff"),
          )
          timeline.render_notebook()

          2.繪制動(dòng)態(tài)柱狀圖

          def?get_year_chart(year:?int):
          ????map_data?=?[i["data"]?for?i?in?datas?if?i["time"]==year][0]
          ????min_data,?max_data?=?(
          ????????min([d[1]?for?d?in?map_data]),
          ????????max([d[1]?for?d?in?map_data]),
          ????)
          ????bar_x_data?=?[x[0]?for?x?in?map_data]
          ????bar_y_data?=?[x[1]?for?x?in?map_data]
          ????bar?=?(
          ????????????Bar(init_opts=opts.InitOpts(theme=ThemeType.DARK))
          ????????????.add_xaxis(xaxis_data=bar_x_data)
          ????????????.add_yaxis(
          ????????????????series_name="",
          ????????????????yaxis_data=bar_y_data,
          ????????????????label_opts=opts.LabelOpts(
          ????????????????????is_show=True,?position="right",?formatter=":?{c}"
          ????????????????),
          ????????????)
          ????????????.reversal_axis()
          ????????????.set_global_opts(
          ????????????????title_opts=opts.TitleOpts(
          ????????????????????title="2000年以來中國各省GDP排名變化情況",
          ????????????????????subtitle="GDP單位:億元",
          ????????????????????pos_left="center",
          ????????????????????pos_top="top",
          ????????????????????title_textstyle_opts=opts.TextStyleOpts(
          ????????????????????????font_size=25,?color="rgba(123,104,238,?0.9)"
          ????????????????????),
          ????????????????),
          ????????????????xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(is_show=False)),
          ????????????????yaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(is_show=False)),
          ????????????????tooltip_opts=opts.TooltipOpts(is_show=False),
          ????????????????visualmap_opts=opts.VisualMapOpts(
          ????????????????????is_calculable=True,
          ????????????????????dimension=0,
          ????????????????????pos_left="10",
          ????????????????????pos_top="center",
          ????????????????????range_text=["High",?"Low"],
          ????????????????????range_color=["lightskyblue",?"yellow",?"orangered"],
          ????????????????????textstyle_opts=opts.TextStyleOpts(color="#ddd"),
          ????????????????????min_=min_data,
          ????????????????????max_=max_data,
          ????????????????),
          ????????????)
          ????????)
          ????return?bar
          time_list?=?list(range(2000,2020))
          timeline?=?Timeline(
          ????init_opts=opts.InitOpts(width="1000px",?height="800px",?theme=ThemeType.DARK)
          )
          for?y?in?time_list:
          ????g?=?get_year_chart(year=y)
          ????timeline.add(g,?time_point=str(y))

          timeline.add_schema(
          ????orient="vertical",
          ????is_auto_play=True,
          ????is_inverse=True,
          ????play_interval=500,
          ????pos_left="null",
          ????pos_right="5",
          ????pos_top="20",
          ????pos_bottom="20",
          ????width="50",
          ????label_opts=opts.LabelOpts(is_show=True,?color="#fff"),
          )
          timeline.render_notebook()

          3.繪制動(dòng)態(tài)餅圖

          def?get_year_chart(year:?int):
          ????map_data?=?[i["data"]?for?i?in?datas?if?i["time"]==year][0]
          ????min_data,?max_data?=?(
          ????????min([d[1]?for?d?in?map_data]),
          ????????max([d[1]?for?d?in?map_data]),
          ????)
          ????pie_data?=?[[x[0],?x[1]]?for?x?in?map_data]
          ????percent_sum?=?sum([x[1]?for?x?in?map_data])
          ????rest_value?=?0
          ????for?d?in?map_data:
          ????????rest_percent?=?100.0
          ????????rest_percent?=?rest_percent?-?percent_sum
          ????????rest_value?=?d[1]?*?(rest_percent?/?d[1])
          ????pie?=?(
          ????????Pie(init_opts=opts.InitOpts(theme=ThemeType.DARK))
          ????????.add(
          ????????????series_name="",
          ????????????data_pair=pie_data,
          ????????????radius=["12%",?"20%"],
          ????????????center=["50%",?"50%"],
          ????????????itemstyle_opts=opts.ItemStyleOpts(
          ????????????????border_width=1,?border_color="rgba(0,0,0,0.3)"
          ????????????),
          ????????)
          ????????.set_global_opts(
          ????????????title_opts=opts.TitleOpts(
          ????????????????????title="2000年以來中國各省GDP排名變化情況",
          ????????????????????subtitle="GDP單位:億元",
          ????????????????????pos_left="center",
          ????????????????????pos_top="top",
          ????????????????????title_textstyle_opts=opts.TextStyleOpts(
          ????????????????????????font_size=25,?color="rgba(123,104,238,?0.9)"
          ????????????????????),
          ????????????????),
          ????????????tooltip_opts=opts.TooltipOpts(is_show=True,?formatter="?go7utgvlrp%"),
          ????????????legend_opts=opts.LegendOpts(is_show=False),
          ????????)
          ????)
          ????return?pie
          time_list?=?list(range(2000,2020))
          timeline?=?Timeline(
          ????init_opts=opts.InitOpts(width="1000px",?height="800px",?theme=ThemeType.DARK)
          )
          for?y?in?time_list:
          ????g?=?get_year_chart(year=y)
          ????timeline.add(g,?time_point=str(y))

          timeline.add_schema(
          ????orient="vertical",
          ????is_auto_play=True,
          ????is_inverse=True,
          ????play_interval=500,
          ????pos_left="null",
          ????pos_right="5",
          ????pos_top="20",
          ????pos_bottom="20",
          ????width="50",
          ????label_opts=opts.LabelOpts(is_show=True,?color="#fff"),
          )
          timeline.render_notebook()

          三、合并動(dòng)圖

          最后,通過grid模塊將三個(gè)圖形合并到一起:


          文中數(shù)據(jù)、完整代碼已經(jīng)打包整理完畢,請(qǐng)?jiān)诠娞?hào)Python小二后臺(tái)回復(fù)201220獲取
          < END >

          瀏覽 70
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  无a毛片| 东京热一区二区三区四区 | 九九精品久久久久久久久无码人妻 | 女人色毛片女人色毛片18 | 天天干人人 |