柱状图的构建-Python

师从黑马程序员

基础柱状图的构建

通过Bar构建基础柱状图

反转x和y轴

数值标签放在右侧

from pyecharts.charts import Bar
from pyecharts.options import LabelOpts
#使用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")

基础时间线柱状图

创建时间线

自动播放

时间线设置主题

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",[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,60,60],label_opts=LabelOpts(position="right"))
bar3.reversal_axis()

#构造时间线对象
timeline=Timeline({"theme":ThemeType.LIGHT})
#在时间线内部添加柱状图对象
timeline.add(bar1,"点1")
timeline.add(bar2,"点2")
timeline.add(bar3,"点3")
#自动播放设置
timeline.add_schema(
    play_interval=750,
    is_timeline_show=False,
    is_auto_play=True,
    is_loop_play=True
)
#主题设置

#绘图 #绘图使用时间线对象绘图,二不是bar对象了
timeline.render("基础时间线柱状图.html")

GDP动态柱状图绘制

列表的sort方法

my_list=[["a",33],["b",55],["c",11]]

#带名函数
# def choose_sort_key(element):
#     return element[1]#意思是以下标为1的元素作为排序的依据
# my_list.sort(key=choose_sort_key,reverse=True)
# #默认排序方法为从小到大,reverse=True使排序从大到小
# print(my_list)

#匿名函数
my_list.sort(key=lambda element:element[1],reverse=True)
print(my_list)

需求分析

处理数据

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

f=open("D:/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 = str(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.LIGHT})
#排序年份
sorted_year_list=sorted(data_dict.keys())
for year in sorted_year_list:
    #排序GDP
    data_dict[year].sort(key=lambda element:element[1],reverse=True)
    #取出本年份前8名的国家
    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}年全球前8GDP数据")
    )
    timeline.add(bar,str(year))#bar是数据,名字是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("1960-2019全球GDP前8的国家.html")

若有侵权,请联系作者

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

乘~风

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

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

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

打赏作者

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

抵扣说明:

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

余额充值