自学内容网 自学内容网

python——Echarts现交互式动态可视化

数据展示

20192018201720162015201420132012
北京5817.15785.91765430.78755081.264723.864027.16093661.10973314.934
天津2410.252106.23972310.35522723.52667.112390.35182079.07161760.0201
河北3742.673513.86433233.83322849.872649.182446.61662295.62032084.2825
山西2347.562292.69821867.002215571642.351820.6351701.62271516.378
内蒙古2059.741857.64931703.20952016.431964.481843.67361720.98431552.7453
辽宁2651.962616.08322392.76532200.492127.393192.78133343.81063105.3785
吉林1116.861240.88921210.90811263.781229.351203.38431156.96161041.2514
黑龙江1262.641282.5951243.31181148.411165.881301.3121277.39511163.1708
上海7165.17108.1486642.26386406.135519.54585.55344109.50863743.7053
江苏8802.368630.16058171.53158121.238028.597233.14266568.4645860.6884
浙江70486598.2125804.38375301.984809.944122.02113796.92413441.2267
安徽3182.543048.67052812.44952672.792454.32218.44182075.0751792.7192
福建3052.723007.40872809.03322654.832544.242362.21382119.44551776.1728
江西2486.512373.0082247.06242151.472165.741881.83151621.23581371.994
山东6526.646485.39596098.63245860.185529.335026.82734559.94634059.4301
河南4041.63766.01833407.21873153.473016.052739.25562415.44822040.331
湖北3388.393307.07823248.31593102.063005.532566.89532191.22211823.0532
湖南3006.992860.84432757.82122697.882515.432262.78592030.87581782.156
广东12651.4612105.255211320.349710390.359366.788065.07587081.46556229.1804
广西1811.891681.44661615.12731556.271515.161422.28031317.60351166.0614
海南814.13752.6673674.105637.51627.7555.3064481.014409.437
重庆2134.882265.54212252.37882227.912154.831922.01591693.24381703.4885
四川4070.693911.00923577.98873388.853355.443061.06842784.09522421.2703
贵州1767.361726.85161613.83771561.341503.381366.67311206.41461014.0547
云南2073.531994.34581886.16871812.291808.11698.05741611.29551338.1509
西藏222230.3543185.8341155.99137.13124.270895.023786.5827
陕西2287.732243.13912006.69391833.992059.951890.40441748.33051600.6862
甘肃850.23871.0537815.7323786.97743.86672.6698607.2717520.3993
青海282.14272.887246.1961238.51267.13251.6759223.8586186.4165
宁夏423.55436.5205417.5888387.66373.4339.8627308.3376263.9569
新疆1577.61531.42291466.51891298.951330.91282.33671128.4875908.9655

2011201020092008200720062005
3006.282353.93012026.80891837.32381492.6381117.1514919.2098
1455.131068.8093821.9916675.6186540.439417.0479331.8507
1737.771331.85471067.1231947.5858789.1198620.534515.7017
1213.43969.6652805.8279748.0047597.887583.3752368.3437
1356.671069.9776850.8588650.6764492.3615343.3774277.4553
2643.152004.83521591.21971356.08121082.6948817.6718675.2768
850.1602.4092487.0943422.7961320.6892245.2045207.152
997.55755.5788641.6627578.2773440.4689386.844318.2056
3429.832873.5842540.29752358.74642074.47921576.07421417.3976
5148.914079.85953228.782731.40742237.72761656.6821322.6753
3150.82608.46552142.51311933.3891649.49811298.20441066.5964
1463.561149.3952863.9175724.6197543.6973428.0265334.017
1501.511151.4923932.4282833.4032699.4577541.1707432.6003
1053.43778.0922581.3012488.6476389.851305.5214252.9236
3455.932749.38422198.63241957.05411675.3981356.25261073.125
1721.761381.31781126.06381008.9009862.0804679.1715537.6514
1526.911011.2314814.8653710.8492590.3552476.0823375.5217
1517.071081.6901847.6178722.7122606.5508477.9274395.2651
5514.844517.04453649.8113310.32352785.80072179.46081807.2044
947.72771.9918620.9888518.4245418.8265342.5788283.0359
340.12270.9915178.242144.8584108.293581.813968.6802
1488.33952.0745655.1701577.5738442.7317.7165256.8072
2044.791561.67271174.59271041.6603850.8606607.585479.6635
773.08533.7309416.4761347.8416285.1375226.8157182.4963
1111.16871.1875698.2525614.0518486.7146379.9702312.649
54.7636.647330.089424.882320.141214.560712.0312
1500.18958.2065735.2704591.475475.2398362.4805275.3183
450.12353.5833286.5898264.965190.9107141.2152123.5026
151.81110.215387.738171.569256.708342.243733.8222
219.98153.5507111.575595.00980.031261.35747.7216
720.43500.5759388.7848361.0616285.86219.4628180.3184
20042003200220012000
744.4874592.5388533.99454.1676344.9968
246.18204.5295209.7021163.635133.6069
407.8273335.8263302.3068283.5023248.7621
256.3634186.0547150.8245132.7618114.4762
196.7589138.7157112.854699.431395.032
529.6405447.049399.6888370.4387295.6274
166.2807154.0033131.4885121.1015103.8267
289.42248.8643231.8908213.6398185.3379
1106.1932886.2277719.79609.4719485.3777
980.4939798.1065643.6966572.1473448.3097
805.9479706.5607566.8522500.6948342.7745
274.6284220.7487200.2154192.1813178.7187
333.523304.7095272.8867274.2846234.1061
205.7667168.167140.5457131.979111.5536
828.3306713.7877610.2242573.1793463.6788
428.7799338.0535296.7179267.7459246.4694
310.4464259.7636243.4403231.941214.345
320.6279268.6469231.1459205.4078177.0403
1418.50561315.51511201.61261160.5126910.556
237.7721203.6578186.732178.6706147.0539
57.035851.320546.238543.765639.1995
200.6241161.5618157.8651106.124387.2442
385.7848336.5917291.8746271.1245233.863
149.2855124.5552108.2899.749485.2324
263.3618228.9992206.7594191.2799180.745
10.01888.14997.30826.11085.3848
214.9586177.33150.2934135.8109114.9711
104.1687.656176.243269.948561.2849
26.99624.041121.096519.824116.5843
37.467730.03126.471427.574520.8244
155.704128.2218116.472495.093379.0724

在 Python 中,可以使用 Echarts 来实现交互式动态可视化。Echarts 是一个强大的可视化库,能够创建各种精美的图表和可视化效果。

以下是实现步骤:

一、安装必要的库

首先,确保你已经安装了 Python 和以下库:

  • pyecharts:用于在 Python 中生成 Echarts 图表。

可以使用以下命令安装 pyecharts

pip install pyecharts

二、创建图表

  1. 导入所需的模块:
from pyecharts.charts import Bar
from pyecharts import options as opts
  1. 创建一个柱状图示例:
bar = Bar()
bar.add_xaxis(["A", "B", "C", "D", "E"])
bar.add_yaxis("Series 1", [10, 20, 30, 40, 50])
bar.set_global_opts(title_opts=opts.TitleOpts(title="Sample Bar Chart"))

三、渲染图表

  1. 可以将图表渲染为 HTML 文件:
bar.render("bar_chart.html")
  1. 也可以在 Jupyter Notebook 中直接显示图表:
bar.render_notebook()

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('全国各省财政收入.xlsx',index_col=0)

years=list(data.keys()) #获取列名
citys=list(data.index)    #获取索引行名
citys=[city.replace('省','').replace('市','').replace('自治区','') for city in citys]
datas=[]
for time in years:
    dict_year={}
    dict_year['time']=time
    data_list=[[i,j] for i,j in zip(citys,list(data[time]))]
    dict_year['data']=sorted(data_list, key=(lambda x: x[1]),reverse=True)
    datas.append(dict_year)
def get_year_chart(year: int):
    map_data = [datasDict["data"] for datasDict in datas if datasDict["time"]==year][0][:10]
    print(map_data)
    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))## 实例化一个Map对象
            .add(
                series_name="",
                data_pair=map_data,
                label_opts=opts.LabelOpts(is_show=True),
                is_map_symbol_show=False,
                maptype="china"
            )#创建地图
            #Map图表中add基本参数为:add(‘图例名称’,date,maptype),maptype选项分为:全球:world;中国:china;
            #set_series_opts(label_opts=opts.LabelOpts(is_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"],#visualMap 组件过渡颜色 (可以自定义)(颜色从淡到深)
                    textstyle_opts=opts.TextStyleOpts(color="#ddd"),#文字样式配置项
                    min_=min_data,
                    max_=max_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="",
                y_axis=bar_y_data,
                label_opts=opts.LabelOpts(
                    is_show=True, position="right", formatter="{b}: {c}"
                ),
            )
            .reversal_axis()#横向柱状图展示
            .set_global_opts(
                xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(is_show=False)),#对X轴和Y轴标上名字
                yaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(is_show=False)),#柱状图X轴和Y轴上面的个性化标记,旋转坐标轴: 解决坐标轴名字过长的问题
                tooltip_opts=opts.TooltipOpts(is_show=True),#要设置柱形图上面的提示框,这个时候需要用到TooltipOpts。
                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,
                ),
            )
        )
    pie_data = [[x[0], x[1]] for x in map_data]
    percent_sum = sum([x[1] for x in map_data])
    print(percent_sum)
    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=["80%", "90%"],#饼图的中心(圆心)坐标,数组的第一项是横坐标,第二项是纵坐标
            itemstyle_opts=opts.ItemStyleOpts(
                border_width=1, border_color="rgba(0,0,0,0.3)"#RGBA,比如'rgba(128, 128, 128, 0.5),0.5参数通道表示不透明度
            ),## 图元样式配置项
        )
        .set_global_opts(
            tooltip_opts=opts.TooltipOpts(is_show=True, formatter="{b} {d}%"),
            legend_opts=opts.LegendOpts(is_show=False),
        )
    )
    grid_chart = (
        Grid()#Grid 类:并行显示多张图
        .add(
            bar,
            grid_opts=opts.GridOpts(
                pos_left="10", pos_right="45%", pos_top="70%", pos_bottom="5"
            ),
        )
        .add(pie, grid_opts=opts.GridOpts())
        .add(map_chart, grid_opts=opts.GridOpts())
    )

    return grid_chart
# 生成时间轴实现轮播
time_list = list(range(2000,2020))
#timeline本身是时间轴,Timeline 类:提供时间线轮播多张图
timeline = Timeline(
    init_opts=opts.InitOpts(width="900px", height="400px", theme=ThemeType.DARK)
)
for time in time_list:
    g = get_year_chart(year=time)
    timeline.add(g, time_point=str(time))
timeline.add_schema(
    orient="vertical",
    is_auto_play=True,#是否自动播放。
    is_inverse=True,##是否反向放置 timeline,反向则首位颠倒过来
    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(path='test_bar.html')

import pyecharts.options as opts
from pyecharts.charts import Timeline, Map
import pandas as pd

# 读取 Excel 文件中的数据(假设你的 Excel 文件中包含省份和对应年份的 GDP 数据)
data = pd.read_excel('全国各省财政收入.xlsx', index_col=0)
years = list(data.keys())
citys = list(data.index)
citys = [city.replace('省', '').replace('市', '').replace('自治区', '') for city in citys]

# 重新组织数据
total_data = {}
for year in years:
    temp = list(data[year])
    total_data[year] = []
    for i in range(len(temp)):
        total_data[year].append({"name": citys[i], "value": temp[i]})


# 获取指定年份的中国地图图表
def get_map_chart(year: int) -> Map:
    map_chart = (
        Map()
      .add("GDP", total_data[year], "china")
      .set_global_opts(
            title_opts=opts.TitleOpts(title=f"{year}年各省 GDP 分布"),
            visualmap_opts=opts.VisualMapOpts(max_=max([item["value"] for item in total_data[year]]),
                                              min_=min([item["value"] for item in total_data[year]]))
        )
    )
    return map_chart


# 生成时间轴的图
timeline = Timeline()

for y in years:
    timeline.add(get_map_chart(year=y), time_point=str(y))

# 设置时间轴自动播放等属性
timeline.add_schema(is_auto_play=True, play_interval=1000)
timeline.render("gdp_china_map_timeline.html")

 

import pyecharts.options as opts
from pyecharts.charts import Timeline, Bar, Pie
import pandas as pd


# 读取 Excel 文件中的数据
data = pd.read_excel('全国各省财政收入.xlsx', index_col=0)
years = list(data.keys())
citys = list(data.index)
citys = [city.replace('省', '').replace('市', '').replace('自治区', '') for city in citys]

# 重新组织数据
total_data = {}
for year in years:
    temp = list(data[year])
    total_data[year] = []
    for i in range(len(temp)):
        total_data[year].append({"name": citys[i], "value": temp[i]})

    max_data = max(temp)
    sum_data = sum(temp)
    total_data[str(year) + "max"] = int(max_data / 100) * 100
    total_data[str(year) + "sum"] = sum_data


# 获取指定年份的图表
def get_year_overlap_chart(year: int) -> Bar:
    bar = (
        Bar()
       .add_xaxis(xaxis_data=[item["name"] for item in total_data[year]])
       .add_yaxis(
            series_name="财政收入",
            y_axis=[item["value"] for item in total_data[year]],
            label_opts=opts.LabelOpts(is_show=False),
        )
       .set_global_opts(
            title_opts=opts.TitleOpts(
                title="{}年全国各省财政收入".format(year), subtitle="数据来源:全国各省财政收入.xlsx"
            ),
            tooltip_opts=opts.TooltipOpts(
                is_show=True, trigger="axis", axis_pointer_type="shadow"
            ),
            legend_opts=opts.LegendOpts(
                selected_map={
                    "财政收入": False,
                }
            ),
        )
    )
    return bar


# 生成时间轴的图
timeline = Timeline()

for y in years:
    timeline.add(get_year_overlap_chart(year=y), time_point=str(y))

# 设置时间轴自动播放等属性
timeline.add_schema(is_auto_play=True, play_interval=1000)
timeline.render("finance_indices_2003.html")

 

 

import pyecharts.options as opts
from pyecharts.charts import Pie
import pandas as pd
import pandas as pd
from pyecharts.charts import Bar, Scatter
from pyecharts import options as opts

# 读取 Excel 文件中的数据
data = pd.read_excel('全国各省财政收入.xlsx', index_col=0)
years = list(data.keys())
citys = list(data.index)
citys = [city.replace('省', '').replace('市', '').replace('自治区', '') for city in citys]

# 重新组织数据
total_data = {}
for year in years:
    temp = list(data[year])
    total_data[year] = []
    for i in range(len(temp)):
        total_data[year].append({"name": citys[i], "value": temp[i]})

# 选择某一年份(这里以第一年为例)来绘制饼图
year = years[0]
data_for_pie = total_data[year]

# 获取饼图
def get_pie_chart() -> Pie:
    pie = (
        Pie()
      .add(
            "",
            [(item["name"], item["value"]) for item in data_for_pie],
            radius=["30%", "75%"],
        )
      .set_global_opts(
            title_opts=opts.TitleOpts(title=f"{year}年全国各省财政收入饼图"),
            tooltip_opts=opts.TooltipOpts(trigger="item", formatter="{a} <br/>{b}: {c} ({d}%)"),
        )
      .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}"))
    )
    return pie

get_pie_chart().render("pie_chart.html")

 


原文地址:https://blog.csdn.net/weixin_66547608/article/details/142750464

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!