数据可视化pyecharts——数据分析(柱状图、折线图、饼图)

安装

首先确保已经安装了pyecharts库,如果没有,可以通过pip install pyecharts进行安装。

柱状图

  • pyecharts.charts导入Bar,从pyecharts导入options
  • 准备数据(如类别数据x_data和对应的数值数据y_data)。
  • 创建Bar对象,通过add_xaxis添加类别数据,add_yaxis添加数据系列,可设置系列名称。
  • 可以使用set_global_opts设置全局配置项,如标题等,最后使用render方法生成图表文件。

折线图

  • pyecharts.charts导入Line
  • 类似柱状图,准备数据后创建Line对象,添加轴数据。
  • 能设置线条样式、标记点等个性化配置,再通过全局配置项设置相关属性,最后渲染。

饼图

  • pyecharts.charts导入Pie
  • 准备数据(通常是数据和对应的标签)。
  • 创建Pie对象,通过add方法添加数据系列,可设置半径等参数。
  • 同样可配置全局选项,如标题、图例等,完成后进行渲染。

通过这些步骤,可以利用 pyecharts 方便地实现柱状图展示数据对比、折线图展示数据趋势、饼图展示数据占比等数据可视化操作。

数据展示

|---------|----------|------------|------------|----------|----------|-----------|-----------|-----------|----------|-----------|-----------|-----------|-----------|-----------|-----------|-----------|-----------|-----------|-----------|----------|
| | 2019 | 2018 | 2017 | 2016 | 2015 | 2014 | 2013 | 2012 | 2011 | 2010 | 2009 | 2008 | 2007 | 2006 | 2005 | 2004 | 2003 | 2002 | 2001 | 2000 |
| 北京 | 5817.1 | 5785.9176 | 5430.7875 | 5081.26 | 4723.86 | 4027.1609 | 3661.1097 | 3314.934 | 3006.28 | 2353.9301 | 2026.8089 | 1837.3238 | 1492.638 | 1117.1514 | 919.2098 | 744.4874 | 592.5388 | 533.99 | 454.1676 | 344.9968 |
| 天津 | 2410.25 | 2106.2397 | 2310.3552 | 2723.5 | 2667.11 | 2390.3518 | 2079.0716 | 1760.0201 | 1455.13 | 1068.8093 | 821.9916 | 675.6186 | 540.439 | 417.0479 | 331.8507 | 246.18 | 204.5295 | 209.7021 | 163.635 | 133.6069 |
| 河北 | 3742.67 | 3513.8643 | 3233.8332 | 2849.87 | 2649.18 | 2446.6166 | 2295.6203 | 2084.2825 | 1737.77 | 1331.8547 | 1067.1231 | 947.5858 | 789.1198 | 620.534 | 515.7017 | 407.8273 | 335.8263 | 302.3068 | 283.5023 | 248.7621 |
| 山西 | 2347.56 | 2292.6982 | 1867.0022 | 1557 | 1642.35 | 1820.635 | 1701.6227 | 1516.378 | 1213.43 | 969.6652 | 805.8279 | 748.0047 | 597.887 | 583.3752 | 368.3437 | 256.3634 | 186.0547 | 150.8245 | 132.7618 | 114.4762 |
| 内蒙古 | 2059.74 | 1857.6493 | 1703.2095 | 2016.43 | 1964.48 | 1843.6736 | 1720.9843 | 1552.7453 | 1356.67 | 1069.9776 | 850.8588 | 650.6764 | 492.3615 | 343.3774 | 277.4553 | 196.7589 | 138.7157 | 112.8546 | 99.4313 | 95.032 |
| 辽宁 | 2651.96 | 2616.0832 | 2392.7653 | 2200.49 | 2127.39 | 3192.7813 | 3343.8106 | 3105.3785 | 2643.15 | 2004.8352 | 1591.2197 | 1356.0812 | 1082.6948 | 817.6718 | 675.2768 | 529.6405 | 447.049 | 399.6888 | 370.4387 | 295.6274 |
| 吉林 | 1116.86 | 1240.8892 | 1210.9081 | 1263.78 | 1229.35 | 1203.3843 | 1156.9616 | 1041.2514 | 850.1 | 602.4092 | 487.0943 | 422.7961 | 320.6892 | 245.2045 | 207.152 | 166.2807 | 154.0033 | 131.4885 | 121.1015 | 103.8267 |
| 黑龙江 | 1262.64 | 1282.595 | 1243.3118 | 1148.41 | 1165.88 | 1301.312 | 1277.3951 | 1163.1708 | 997.55 | 755.5788 | 641.6627 | 578.2773 | 440.4689 | 386.844 | 318.2056 | 289.42 | 248.8643 | 231.8908 | 213.6398 | 185.3379 |
| 上海 | 7165.1 | 7108.148 | 6642.2638 | 6406.13 | 5519.5 | 4585.5534 | 4109.5086 | 3743.7053 | 3429.83 | 2873.584 | 2540.2975 | 2358.7464 | 2074.4792 | 1576.0742 | 1417.3976 | 1106.1932 | 886.2277 | 719.79 | 609.4719 | 485.3777 |
| 江苏 | 8802.36 | 8630.1605 | 8171.5315 | 8121.23 | 8028.59 | 7233.1426 | 6568.464 | 5860.6884 | 5148.91 | 4079.8595 | 3228.78 | 2731.4074 | 2237.7276 | 1656.682 | 1322.6753 | 980.4939 | 798.1065 | 643.6966 | 572.1473 | 448.3097 |
| 浙江 | 7048 | 6598.212 | 5804.3837 | 5301.98 | 4809.94 | 4122.0211 | 3796.9241 | 3441.2267 | 3150.8 | 2608.4655 | 2142.5131 | 1933.389 | 1649.4981 | 1298.2044 | 1066.5964 | 805.9479 | 706.5607 | 566.8522 | 500.6948 | 342.7745 |
| 安徽 | 3182.54 | 3048.6705 | 2812.4495 | 2672.79 | 2454.3 | 2218.4418 | 2075.075 | 1792.7192 | 1463.56 | 1149.3952 | 863.9175 | 724.6197 | 543.6973 | 428.0265 | 334.017 | 274.6284 | 220.7487 | 200.2154 | 192.1813 | 178.7187 |
| 福建 | 3052.72 | 3007.4087 | 2809.0332 | 2654.83 | 2544.24 | 2362.2138 | 2119.4455 | 1776.1728 | 1501.51 | 1151.4923 | 932.4282 | 833.4032 | 699.4577 | 541.1707 | 432.6003 | 333.523 | 304.7095 | 272.8867 | 274.2846 | 234.1061 |
| 江西 | 2486.51 | 2373.008 | 2247.0624 | 2151.47 | 2165.74 | 1881.8315 | 1621.2358 | 1371.994 | 1053.43 | 778.0922 | 581.3012 | 488.6476 | 389.851 | 305.5214 | 252.9236 | 205.7667 | 168.167 | 140.5457 | 131.979 | 111.5536 |
| 山东 | 6526.64 | 6485.3959 | 6098.6324 | 5860.18 | 5529.33 | 5026.8273 | 4559.9463 | 4059.4301 | 3455.93 | 2749.3842 | 2198.6324 | 1957.0541 | 1675.398 | 1356.2526 | 1073.125 | 828.3306 | 713.7877 | 610.2242 | 573.1793 | 463.6788 |
| 河南 | 4041.6 | 3766.0183 | 3407.2187 | 3153.47 | 3016.05 | 2739.2556 | 2415.4482 | 2040.331 | 1721.76 | 1381.3178 | 1126.0638 | 1008.9009 | 862.0804 | 679.1715 | 537.6514 | 428.7799 | 338.0535 | 296.7179 | 267.7459 | 246.4694 |
| 湖北 | 3388.39 | 3307.0782 | 3248.3159 | 3102.06 | 3005.53 | 2566.8953 | 2191.2221 | 1823.0532 | 1526.91 | 1011.2314 | 814.8653 | 710.8492 | 590.3552 | 476.0823 | 375.5217 | 310.4464 | 259.7636 | 243.4403 | 231.941 | 214.345 |
| 湖南 | 3006.99 | 2860.8443 | 2757.8212 | 2697.88 | 2515.43 | 2262.7859 | 2030.8758 | 1782.156 | 1517.07 | 1081.6901 | 847.6178 | 722.7122 | 606.5508 | 477.9274 | 395.2651 | 320.6279 | 268.6469 | 231.1459 | 205.4078 | 177.0403 |
| 广东 | 12651.46 | 12105.2552 | 11320.3497 | 10390.35 | 9366.78 | 8065.0758 | 7081.4655 | 6229.1804 | 5514.84 | 4517.0445 | 3649.811 | 3310.3235 | 2785.8007 | 2179.4608 | 1807.2044 | 1418.5056 | 1315.5151 | 1201.6126 | 1160.5126 | 910.556 |
| 广西 | 1811.89 | 1681.4466 | 1615.1273 | 1556.27 | 1515.16 | 1422.2803 | 1317.6035 | 1166.0614 | 947.72 | 771.9918 | 620.9888 | 518.4245 | 418.8265 | 342.5788 | 283.0359 | 237.7721 | 203.6578 | 186.732 | 178.6706 | 147.0539 |
| 海南 | 814.13 | 752.6673 | 674.105 | 637.51 | 627.7 | 555.3064 | 481.014 | 409.437 | 340.12 | 270.9915 | 178.242 | 144.8584 | 108.2935 | 81.8139 | 68.6802 | 57.0358 | 51.3205 | 46.2385 | 43.7656 | 39.1995 |
| 重庆 | 2134.88 | 2265.5421 | 2252.3788 | 2227.91 | 2154.83 | 1922.0159 | 1693.2438 | 1703.4885 | 1488.33 | 952.0745 | 655.1701 | 577.5738 | 442.7 | 317.7165 | 256.8072 | 200.6241 | 161.5618 | 157.8651 | 106.1243 | 87.2442 |
| 四川 | 4070.69 | 3911.0092 | 3577.9887 | 3388.85 | 3355.44 | 3061.0684 | 2784.0952 | 2421.2703 | 2044.79 | 1561.6727 | 1174.5927 | 1041.6603 | 850.8606 | 607.585 | 479.6635 | 385.7848 | 336.5917 | 291.8746 | 271.1245 | 233.863 |
| 贵州 | 1767.36 | 1726.8516 | 1613.8377 | 1561.34 | 1503.38 | 1366.6731 | 1206.4146 | 1014.0547 | 773.08 | 533.7309 | 416.4761 | 347.8416 | 285.1375 | 226.8157 | 182.4963 | 149.2855 | 124.5552 | 108.28 | 99.7494 | 85.2324 |
| 云南 | 2073.53 | 1994.3458 | 1886.1687 | 1812.29 | 1808.1 | 1698.0574 | 1611.2955 | 1338.1509 | 1111.16 | 871.1875 | 698.2525 | 614.0518 | 486.7146 | 379.9702 | 312.649 | 263.3618 | 228.9992 | 206.7594 | 191.2799 | 180.745 |
| 西藏 | 222 | 230.3543 | 185.8341 | 155.99 | 137.13 | 124.2708 | 95.0237 | 86.5827 | 54.76 | 36.6473 | 30.0894 | 24.8823 | 20.1412 | 14.5607 | 12.0312 | 10.0188 | 8.1499 | 7.3082 | 6.1108 | 5.3848 |
| 陕西 | 2287.73 | 2243.1391 | 2006.6939 | 1833.99 | 2059.95 | 1890.4044 | 1748.3305 | 1600.6862 | 1500.18 | 958.2065 | 735.2704 | 591.475 | 475.2398 | 362.4805 | 275.3183 | 214.9586 | 177.33 | 150.2934 | 135.8109 | 114.9711 |
| 甘肃 | 850.23 | 871.0537 | 815.7323 | 786.97 | 743.86 | 672.6698 | 607.2717 | 520.3993 | 450.12 | 353.5833 | 286.5898 | 264.965 | 190.9107 | 141.2152 | 123.5026 | 104.16 | 87.6561 | 76.2432 | 69.9485 | 61.2849 |
| 青海 | 282.14 | 272.887 | 246.1961 | 238.51 | 267.13 | 251.6759 | 223.8586 | 186.4165 | 151.81 | 110.2153 | 87.7381 | 71.5692 | 56.7083 | 42.2437 | 33.8222 | 26.996 | 24.0411 | 21.0965 | 19.8241 | 16.5843 |
| 宁夏 | 423.55 | 436.5205 | 417.5888 | 387.66 | 373.4 | 339.8627 | 308.3376 | 263.9569 | 219.98 | 153.5507 | 111.5755 | 95.009 | 80.0312 | 61.357 | 47.7216 | 37.4677 | 30.031 | 26.4714 | 27.5745 | 20.8244 |
| 新疆 | 1577.6 | 1531.4229 | 1466.5189 | 1298.95 | 1330.9 | 1282.3367 | 1128.4875 | 908.9655 | 720.43 | 500.5759 | 388.7848 | 361.0616 | 285.86 | 219.4628 | 180.3184 | 155.704 | 128.2218 | 116.4724 | 95.0933 | 79.0724 |

数据引用处理:

python 复制代码
# 读取 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]

重新组织数据

python 复制代码
# 重新组织数据
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]})
print(total_data)

选取合适代码,进行画图:

对应的代码:

折线图

python 复制代码
# 获取指定年份的图表
def get_year_overlap_chart(year: int) -> Line:
    line = (
        Line()
      .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 line


# 生成时间轴的图
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_2002.html")

柱状图

python 复制代码
# 获取指定年份的图表
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_2002.html")
#%%
from pyecharts.charts import Line, Timeline
from pyecharts import options as opts


# 获取指定年份的图表
def get_year_overlap_chart(year: int) -> Line:
    line = (
        Line()
      .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,
                }
            ),
        )

饼图

python 复制代码
# 获取指定年份的图表
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_2002.html")
#%%
from pyecharts.charts import Line, Timeline
from pyecharts import options as opts


# 获取指定年份的图表
def get_year_overlap_chart(year: int) -> Line:
    line = (
        Line()
      .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,
                }
            ),
        )
相关推荐
imbackneverdie14 小时前
只用一天,能写完一篇文献综述吗?
人工智能·信息可视化·aigc·文献综述·文献检索·ai工具·科研工具
小白学大数据17 小时前
抖音搜索页数据批量爬取,多关键词同步采集实现
爬虫·python·数据分析
西贝爱学习18 小时前
智能手机规格与价格数据集
数据分析·数据集
夜郎king19 小时前
水力模型 INP 文件如何导入 QGIS?超详细实操教程
人工智能·数据挖掘·水力模型·qgis水力制图
计算机毕业编程指导师19 小时前
基于Spark的性格行为数据分析与可视化系统源码 毕业设计 选题推荐 毕设选题 数据分析 机器学习 数据挖掘
大数据·python·数据挖掘·数据分析·spark·毕业设计·性格行为
QDYOKR16820 小时前
OKR管理系统怎么选?2026主流OKR工具深度解析
大数据·人工智能·信息可视化·数据挖掘·数据分析
2601_954971131 天前
经济学专业考CDA数据分析师证书值不值?对求职帮助到底有多大
数据挖掘
知识分享小能手1 天前
R语言入门学习教程,从入门到精通,R语言多维数据可视化(12)
学习·信息可视化·r语言
PaperData2 天前
1988-2025年《中国人口和就业统计年鉴》全年份excel+PDF
数据库·人工智能·数据分析·经管
小王毕业啦2 天前
(1990-2024年)个股交易活跃度、个股换手率
大数据·人工智能·数据挖掘·数据分析·区块链·社科数据