Python基础知识:整理16 使用pyecharts实现动态柱状图

1 基础柱状图的构建

from pyecharts.charts import Bar

bar = Bar()

bar.add_xaxis(["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"])
bar.add_yaxis("商家A", [5, 20, 36, 10, 75, 90])
bar.add_yaxis("商家B", [15, 6, 45, 20, 65, 90])


bar.render("./modules/bar_chart.html")


当我们想要x和y轴进行反转时,可以用以下的方式,反转之后数据还是在中间,我们需要将数据的位置放到右端


2 基础时间线柱状图的构建

from pyecharts.charts import Bar, Timeline
from pyecharts.options import LabelOpts
from pyecharts.globals import ThemeType

bar1 = Bar()
bar1.add_xaxis(["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"])
bar1.add_yaxis("商家A", [5, 20, 36, 10, 75, 90], label_opts=LabelOpts(position="right"))
bar1.add_yaxis("商家B", [15, 6, 45, 20, 65, 90], label_opts=LabelOpts(position="right"))  # 反转之后,需要将数字放在右侧
bar1.reversal_axis()

bar2 = Bar()
bar2.add_xaxis(["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"])
bar2.add_yaxis("商家A", [10, 22, 45, 56, 8, 120], label_opts=LabelOpts(position="right"))
bar2.add_yaxis("商家B", [20, 10, 34, 45, 98, 78], label_opts=LabelOpts(position="right"))  # 反转之后,需要将数字放在右侧
bar2.reversal_axis()

# 绘制时间线对象
timeline = Timeline(
    {"theme": ThemeType.LIGHT}  # 设置主题
)
# 添加两个柱状图到时间线中
timeline.add(bar1, "2019")
timeline.add(bar2, "2020")

# 自动播放设置
timeline.add_schema(
    is_auto_play=True,  # 是否自动播放
    is_loop_play=True,  # 是否循环自动播放
    play_interval=1000,   # 自动播放的时间间隔,单位为毫秒
    is_timeline_show=True  # 是否在自动播放的时候显示时间线
)

# 渲染图表并保存为HTML文件
timeline.render("./modules/bar_chart_timeLine.html")


 

3 综合案例(世界GDP动态柱状图)

# -- coding: utf-8 --
# 每年GDP前八的国家:
from pyecharts.charts import Bar, Timeline
from pyecharts.options import *
from pyecharts.globals import ThemeType
# 1. 打开文件
fr = open("./text/1960-2019全球GDP数据.csv", "r", encoding="GB2312")
data_lines = fr.readlines()
fr.close()

# 2. 数据处理
# 删除第一条数据
data1 = data_lines.pop(0)
# print(data1)

# 将数据转换为字典存储,格式为:
# {年份: [[国家1, 该国GDP数据], [国家2, 该国GDP数据], ...], 年份: [[国家2, 该国GDP数据], ...], ...}

# 先定义一个字典对象
data_dict = {}
for line in data_lines:
    year = int(line.split(",")[0])
    country = line.split(",")[1]
    gdp = float(line.split(",")[2])   # 有些数据采用了科学计数法,需要转换为浮点数

    # 判断字典中是否已经存在该年份的数据, 可以使用if 判断,也可以使用异常处理
    try:
        data_dict[year].append([country, gdp])   # 如果有这个key的话,直接append进去
    except KeyError:
        data_dict[year] = []  # 没有则创建
        data_dict[year].append([country, gdp])

# print(data_dict)


# 创建时间线对象
timeline = Timeline(
    {"theme": ThemeType.CHALK}
)

# 3.绘图
# 排序年份
sorted_year_list = sorted(data_dict.keys())   # 可以保证时间线中的年份顺序是正确的
# print(sorted_year_list)

for year in sorted_year_list:
    data_dict[year].sort(key=lambda x: x[1], reverse=True)

    # 取出本年份前八名的国家
    gdp_top8 = data_dict[year][:8]

    x_data = []
    y_data = []

    for country_gdp in gdp_top8:
        x_data.append(country_gdp[0])
        y_data.append(country_gdp[1] / 100000000)


    # 绘图
    bar = Bar()
    # 为了GDP高的在上面,所以我们需要将x、y轴进行反转
    x_data.reverse()
    y_data.reverse()
    bar.add_xaxis(x_data)
    bar.add_yaxis("GDP(亿元)", y_data, label_opts=LabelOpts(position="right"))

    # 反转
    bar.reversal_axis()

    # 设置每一年的图表标题
    bar.set_global_opts(
        title_opts=TitleOpts(title=f"{year}年GDP前八名图表", pos_left="center", pos_top="6%")

    )
    # 构建时间线
    timeline.add(bar, str(year))

"""
    for 循环每一年的数据,基于每一年的数据,创建每一年的bar对象
    然后将bar对象添加到时间线中
    注意:需要对每一年的数据进行排序,取出GDP前8的国家
    注意:需要对x轴的数据进行反转
"""

timeline.add_schema(
    is_auto_play=True,  # 是否自动播放
    is_loop_play=True,  # 是否循环自动播放
    play_interval=1000,  # 自动播放的时间间隔,单位为毫秒
    is_timeline_show=True  # 是否在自动播放的时候显示时间线
)


timeline.render("./modules/GDP8.html")

详细解释:


 



  • 13
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

是小蟹呀^

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

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

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

打赏作者

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

抵扣说明:

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

余额充值