Python---pyecharts histogram case

Reverse the x-axis and y-axis: bar.reversal_axis()
Basic histogram:
# 导包
from pyecharts.charts import Bar
from pyecharts.options import LabelOpts

# 创建柱状图对象
bar = Bar()

# x轴
bar.add_xaxis(["中国", "美国", "英国"])

# y轴
bar.add_yaxis("GDP", [30, 10, 20],label_opts=LabelOpts(position="right"))

# 反转x轴和y轴
bar.reversal_axis()

# 生成图表
bar.render()
Basic timeline histogram:

Note: The timeline bar chart drawing is drawn using the timeline object instead of the bar object.

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

bar1 = Bar()
bar1.add_xaxis(["美国", "中国", "英国"])
bar1.add_yaxis("GDP", [10, 20, 30])
bar1.reversal_axis()

bar2 = Bar()
bar2.add_xaxis(["美国", "中国", "英国"])
bar2.add_yaxis("GDP", [12, 23, 34])
bar2.reversal_axis()

bar3 = Bar()
bar3.add_xaxis(["美国", "中国", "英国"])
bar3.add_yaxis("GDP", [20, 30, 40])
bar3.reversal_axis()

# 构建时间线对象
timeline = Timeline()

# 主题设置
# timeline = Timeline(
#     {"theme": ThemeType.DARK}
# )

# 在时间线内添加柱状图对象
timeline.add(bar1, "点1")
timeline.add(bar2, "点2")
timeline.add(bar3, "点3")

# 自动播放设置
timeline.add_schema(
    play_interval=1000,
    is_timeline_show=True,
    is_auto_play=True,
    is_loop_play=True
)

# 绘图是用时间线对象绘图,而不是bar对象了
timeline.render("基础时间线柱状图.html")
Dynamic timeline histogram:
from pyecharts.charts import Bar, Timeline
from pyecharts.options import *
from pyecharts.globals import ThemeType

# 读文件
f = open("./1960-2019全球GDP数据.csv", "r", encoding="GB2312")
data_lines = f.readlines()
f.close()

# 删除第一行数据
data_lines.pop(0)

# 数据转换为字典
data_dict = {}
for line in data_lines:
    year = int(line.split(",")[0])  # 年
    country = line.split(",")[1]  # 国家
    gdp = float(line.split(",")[2])  # gdp数据
    try:
        data_dict[year].append([country, gdp])
    except KeyError:
        data_dict[year] = []
        data_dict[year].append([country, gdp])
# print(data_dict)

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

# 排序年份
sorted_year_list = sorted(data_dict.keys())
# print(sorted_year_list)
for year in sorted_year_list:
    data_dict[year].sort(key=lambda element: element[1], reverse=True)  # 依据GDP排序
    # 取出本年份前八名的国家
    year_data = data_dict[year][0:8]
    x_data = []
    y_data = []
    for country_gdp in year_data:
        x_data.append(country_gdp[0])  # x轴添加国家
        y_data.append(country_gdp[1] / 100000000)  # y轴添加GDP

    # 构建柱状图
    bar = Bar()
    x_data.reverse()
    y_data.reverse()
    bar.add_xaxis(x_data)
    bar.add_yaxis("GDP(亿)", y_data, label_opts=LabelOpts(position="right"))

    # 反转x轴和y轴
    bar.reversal_axis()

    # 标题
    bar.set_global_opts(
        title_opts=TitleOpts(title=f"{year}年全球前八GDP数据")
    )

    # 添加数据
    timeline.add(bar, str(year))

# 自动播放
timeline.add_schema(
    play_interval=1000,
    is_timeline_show=True,
    is_auto_play=True,
    is_loop_play=False
)

# 生成图表
timeline.render("全国gdp前八国家.html")

Guess you like

Origin blog.csdn.net/weixin_52053631/article/details/133526923