数据可视化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 方便地实现柱状图展示数据对比、折线图展示数据趋势、饼图展示数据占比等数据可视化操作。

数据展示

20192018201720162015201420132012201120102009200820072006200520042003200220012000
北京5817.15785.91765430.78755081.264723.864027.16093661.10973314.9343006.282353.93012026.80891837.32381492.6381117.1514919.2098744.4874592.5388533.99454.1676344.9968
天津2410.252106.23972310.35522723.52667.112390.35182079.07161760.02011455.131068.8093821.9916675.6186540.439417.0479331.8507246.18204.5295209.7021163.635133.6069
河北3742.673513.86433233.83322849.872649.182446.61662295.62032084.28251737.771331.85471067.1231947.5858789.1198620.534515.7017407.8273335.8263302.3068283.5023248.7621
山西2347.562292.69821867.002215571642.351820.6351701.62271516.3781213.43969.6652805.8279748.0047597.887583.3752368.3437256.3634186.0547150.8245132.7618114.4762
内蒙古2059.741857.64931703.20952016.431964.481843.67361720.98431552.74531356.671069.9776850.8588650.6764492.3615343.3774277.4553196.7589138.7157112.854699.431395.032
辽宁2651.962616.08322392.76532200.492127.393192.78133343.81063105.37852643.152004.83521591.21971356.08121082.6948817.6718675.2768529.6405447.049399.6888370.4387295.6274
吉林1116.861240.88921210.90811263.781229.351203.38431156.96161041.2514850.1602.4092487.0943422.7961320.6892245.2045207.152166.2807154.0033131.4885121.1015103.8267
黑龙江1262.641282.5951243.31181148.411165.881301.3121277.39511163.1708997.55755.5788641.6627578.2773440.4689386.844318.2056289.42248.8643231.8908213.6398185.3379
上海7165.17108.1486642.26386406.135519.54585.55344109.50863743.70533429.832873.5842540.29752358.74642074.47921576.07421417.39761106.1932886.2277719.79609.4719485.3777
江苏8802.368630.16058171.53158121.238028.597233.14266568.4645860.68845148.914079.85953228.782731.40742237.72761656.6821322.6753980.4939798.1065643.6966572.1473448.3097
浙江70486598.2125804.38375301.984809.944122.02113796.92413441.22673150.82608.46552142.51311933.3891649.49811298.20441066.5964805.9479706.5607566.8522500.6948342.7745
安徽3182.543048.67052812.44952672.792454.32218.44182075.0751792.71921463.561149.3952863.9175724.6197543.6973428.0265334.017274.6284220.7487200.2154192.1813178.7187
福建3052.723007.40872809.03322654.832544.242362.21382119.44551776.17281501.511151.4923932.4282833.4032699.4577541.1707432.6003333.523304.7095272.8867274.2846234.1061
江西2486.512373.0082247.06242151.472165.741881.83151621.23581371.9941053.43778.0922581.3012488.6476389.851305.5214252.9236205.7667168.167140.5457131.979111.5536
山东6526.646485.39596098.63245860.185529.335026.82734559.94634059.43013455.932749.38422198.63241957.05411675.3981356.25261073.125828.3306713.7877610.2242573.1793463.6788
河南4041.63766.01833407.21873153.473016.052739.25562415.44822040.3311721.761381.31781126.06381008.9009862.0804679.1715537.6514428.7799338.0535296.7179267.7459246.4694
湖北3388.393307.07823248.31593102.063005.532566.89532191.22211823.05321526.911011.2314814.8653710.8492590.3552476.0823375.5217310.4464259.7636243.4403231.941214.345
湖南3006.992860.84432757.82122697.882515.432262.78592030.87581782.1561517.071081.6901847.6178722.7122606.5508477.9274395.2651320.6279268.6469231.1459205.4078177.0403
广东12651.4612105.255211320.349710390.359366.788065.07587081.46556229.18045514.844517.04453649.8113310.32352785.80072179.46081807.20441418.50561315.51511201.61261160.5126910.556
广西1811.891681.44661615.12731556.271515.161422.28031317.60351166.0614947.72771.9918620.9888518.4245418.8265342.5788283.0359237.7721203.6578186.732178.6706147.0539
海南814.13752.6673674.105637.51627.7555.3064481.014409.437340.12270.9915178.242144.8584108.293581.813968.680257.035851.320546.238543.765639.1995
重庆2134.882265.54212252.37882227.912154.831922.01591693.24381703.48851488.33952.0745655.1701577.5738442.7317.7165256.8072200.6241161.5618157.8651106.124387.2442
四川4070.693911.00923577.98873388.853355.443061.06842784.09522421.27032044.791561.67271174.59271041.6603850.8606607.585479.6635385.7848336.5917291.8746271.1245233.863
贵州1767.361726.85161613.83771561.341503.381366.67311206.41461014.0547773.08533.7309416.4761347.8416285.1375226.8157182.4963149.2855124.5552108.2899.749485.2324
云南2073.531994.34581886.16871812.291808.11698.05741611.29551338.15091111.16871.1875698.2525614.0518486.7146379.9702312.649263.3618228.9992206.7594191.2799180.745
西藏222230.3543185.8341155.99137.13124.270895.023786.582754.7636.647330.089424.882320.141214.560712.031210.01888.14997.30826.11085.3848
陕西2287.732243.13912006.69391833.992059.951890.40441748.33051600.68621500.18958.2065735.2704591.475475.2398362.4805275.3183214.9586177.33150.2934135.8109114.9711
甘肃850.23871.0537815.7323786.97743.86672.6698607.2717520.3993450.12353.5833286.5898264.965190.9107141.2152123.5026104.1687.656176.243269.948561.2849
青海282.14272.887246.1961238.51267.13251.6759223.8586186.4165151.81110.215387.738171.569256.708342.243733.822226.99624.041121.096519.824116.5843
宁夏423.55436.5205417.5888387.66373.4339.8627308.3376263.9569219.98153.5507111.575595.00980.031261.35747.721637.467730.03126.471427.574520.8244
新疆1577.61531.42291466.51891298.951330.91282.33671128.4875908.9655720.43500.5759388.7848361.0616285.86219.4628180.3184155.704128.2218116.472495.093379.0724

 数据引用处理:

# 读取 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]})
print(total_data)

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

对应的代码:

折线图

# 获取指定年份的图表
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")

 

柱状图

# 获取指定年份的图表
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,
                }
            ),
        )

 

饼图

# 获取指定年份的图表
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,
                }
            ),
        )

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

张謹礧

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值