第十二章、Python基础综合案例(数据可视化

您所在的位置:网站首页 柱状图显示趋势 第十二章、Python基础综合案例(数据可视化

第十二章、Python基础综合案例(数据可视化

2023-03-23 20:35| 来源: 网络整理| 查看: 265

基础柱状图

1.通过Bar()构建一个柱状图对象

2.和折线图一样,通过add_xaxis()和add_yaxis()添加x和y轴数据

3.通过柱状图对象的:reversal axis(),反转x和y轴

4.通过label_opts=LabelOpts(position="right")设置数值标签在右侧显示

""" 基础柱状图的开发 """ from pyecharts.charts import Bar from pyecharts.options import LabelOpts import os # 使用Bar构建基础柱状图 bar = Bar() # 添加x轴的数据 bar.add_xaxis(["中国","美国","英国"]) # 添加y轴数据 bar.add_yaxis("GDP",[30, 20, 10],label_opts=LabelOpts(position="right")) # 反转x和y轴 bar.reversal_axis() # 绘图 bar.render("基础柱状图.html") os.system("基础柱状图.html")

基础时间线柱状图 什么是时间线?

from pyecharts.charts import Timeline

timeline = Timeline()

自动播放

# 自动播放设置

timeline.add_schema(

play_interval=1000, # 自动播放的时间间隔,单位毫秒

is_timeline_show=True, # 是否在自动播放的时候,显示时间线

is_auto_play=True, # 是否自动播放

#is_loop_play=True # 是否循环播放(默认开启循环)

)

如何设置主题

timeline = Timeline({"theme":ThemeType.__主题__})

""" 带有时间线的柱状图开发 """ from pyecharts.charts import Bar,Timeline from pyecharts.options import LabelOpts from pyecharts.globals import ThemeType import os bar1 = Bar() bar1.add_xaxis(["中国","美国","英国"]) bar1.add_yaxis("GDP",[30, 30, 20],label_opts=LabelOpts(position="right")) bar1.reversal_axis() bar2 = Bar() bar2.add_xaxis(["中国","美国","英国"]) bar2.add_yaxis("GDP",[50, 50, 50],label_opts=LabelOpts(position="right")) bar2.reversal_axis() bar3 = Bar() bar3.add_xaxis(["中国","美国","英国"]) bar3.add_yaxis("GDP",[70, 10, 30],label_opts=LabelOpts(position="right")) bar3.reversal_axis() # 构建时间线对象 timeline = Timeline({"theme":ThemeType.PURPLE_PASSION}) # 在时间线内添加柱状图对象 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") os.system("基础时间线柱状图.html")

GDP动态柱状图绘制 """ 动态GDP柱状图绘制 """ from pyecharts.charts import Bar,Timeline from pyecharts.options import * from pyecharts.globals import ThemeType import os # 读取数据 f = open("D:/BaiduNetdiskDownload/资料/可视化案例数据/动态柱状图数据/1960-2019全球GDP数据.csv","r",encoding="GB2312") data_lines = f.readlines() f.close() # 删除第一条数据 data_lines.pop(0) # 将数据转换为字符串,格式为: # { 年份: [ [国家, gdp], [国家, gdp],....... ], 年份:[ [国家, gdp], [国家, gdp], ......], ......} data_dict = {} for line in data_lines: year = int(line.split(",")[0]) country = line.split(",")[1] gdp = float(line.split(",")[2]) # 判断字典内有无指定的key try: data_dict[year].append([country, gdp]) except KeyError: data_dict[year] = [] data_dict[year].append([country, gdp]) # 创建时间对象 timeline = Timeline({"theme": ThemeType.INFOGRAPHIC}) # 排序年份 sort_year_list = sorted(data_dict.keys()) for year in sort_year_list: data_dict[year].sort(key=lambda element:element[1],reverse=True) # data_dict[year] = data_dict[year][0:8] x_data = [] y_data = [] for county_gdp in y_data: x_data.append(county_gdp[0]) # x轴添加国家 y_data.append(county_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)) # for循环中每一年的数据,基于每一年的数据,创建每一年的bar对象 # 在for中,将每一年的bar对象添加到时间线中 timeline.add_schema( play_interval=1000, is_timeline_show=True, is_auto_play=True, is_loop_play=False ) # 绘图 timeline.render("GDP动态柱状图开发.html") os.system("GDP动态柱状图开发.html")


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3