前言
我們已經(jīng)學(xué)習(xí)了使用 pyecharts
包中的模塊和相應(yīng)的方法繪制了折線(xiàn)圖和地圖,那么今天我將為大家分享如何繪制帶有時(shí)間線(xiàn)的柱狀圖。
如何繪制柱狀圖
繪制柱狀圖跟繪制折線(xiàn)圖的步驟是大致相同的。
python 中繪制柱狀圖依賴(lài)于 pyecharts.charts
模塊下的 Bar
方法。
from pyecharts.charts import Bar
bar = Bar()
x_data = ["英國(guó)","美國(guó)","中國(guó)"]
y_data = [10,20,30]
bar.add_xaxis(x_data)
bar.add_yaxis("GDP",y_data)
bar.render("柱狀圖.html")
我們還可以將橫坐標(biāo)與縱坐標(biāo)顛倒一下,來(lái)使數(shù)據(jù)的展示更加的形象。
使用 bar.reversal_axis()
反轉(zhuǎn)x軸和y軸。
這里數(shù)據(jù)顯示在柱狀圖中,我們看的不是很方便,我也也可以通過(guò)設(shè)置系列配置選項(xiàng)來(lái)使數(shù)據(jù)顯示在柱狀圖的右側(cè)。
bar.add_yaxis("GDP",y_data,label_opts=LabelOpts(position="right"))
所以我們經(jīng)過(guò)完善后的整體代碼是:
from pyecharts.charts import Bar
from pyecharts.options import LabelOpts
bar = Bar()
x_data = ["英國(guó)","美國(guó)","中國(guó)"]
y_data = [10,20,30]
bar.add_xaxis(x_data)
bar.add_yaxis("GDP",y_data,label_opts=LabelOpts(position="right"))
bar.reversal_axis() # 反轉(zhuǎn)x軸和y軸
bar.render("柱狀圖.html")
添加時(shí)間線(xiàn)
通過(guò)添加時(shí)間線(xiàn)我們可以看到多種不同的數(shù)據(jù),每一個(gè)時(shí)間其實(shí)就是一個(gè)柱狀圖,時(shí)間線(xiàn)是由一個(gè)一個(gè)的柱狀圖組成的。
from pyecharts.charts import Bar,Timeline
from pyecharts.options import LabelOpts,TitleOpts
bar1 = Bar()
bar2 = Bar()
bar3 = Bar()
x_data = ["英國(guó)","美國(guó)","中國(guó)"]
y_data1 = [10,20,30]
y_data2 = [20,30,40]
y_data3 = [40,50,70]
bar1.add_xaxis(x_data)
bar1.add_yaxis("GDP",y_data1,label_opts=LabelOpts(position="right"))
bar1.reversal_axis() # 反轉(zhuǎn)x軸和y軸
bar1.set_global_opts(title_opts=TitleOpts(title="2021年GDP"))
bar2.add_xaxis(x_data)
bar2.add_yaxis("GDP",y_data2,label_opts=LabelOpts(position="right"))
bar2.reversal_axis() # 反轉(zhuǎn)x軸和y軸
bar2.set_global_opts(title_opts=TitleOpts(title="2022年GDP"))
bar3.add_xaxis(x_data)
bar3.add_yaxis("GDP",y_data3,label_opts=LabelOpts(position="right"))
bar3.reversal_axis() # 反轉(zhuǎn)x軸和y軸
bar3.set_global_opts(title_opts=TitleOpts(title="2023年GDP"))
timeline = Timeline()
timeline.add(bar1,"2021")
timeline.add(bar2,"2022")
timeline.add(bar3,"2023")
timeline.render("2021-2023中美英三國(guó)GDP.html")
如果我們要讓動(dòng)態(tài)柱狀圖動(dòng)起來(lái)的話(huà),我們需要設(shè)置配置選項(xiàng)。
timeline.add_schema(
play_interval=1000, # 每個(gè)柱狀圖播放間隔時(shí)間,單位(毫秒)
is_timeline_show=True, # 是否顯示時(shí)間線(xiàn),默認(rèn)顯示
is_auto_play=True, # 是否自動(dòng)播放
is_loop_play=True # 是否循環(huán)播放
)
根據(jù)提供的數(shù)據(jù)繪制動(dòng)態(tài)柱狀圖
我們顯示出1960年-2014年全國(guó)GDP數(shù)據(jù)前八的國(guó)家和數(shù)據(jù)。這里提供的數(shù)據(jù)大家可以私信我找我要。
這里提供的數(shù)據(jù)比較簡(jiǎn)單,我們只需要將第一行無(wú)用的數(shù)據(jù)刪除,然后再將這些數(shù)據(jù)轉(zhuǎn)換為我們繪制柱狀圖需要的數(shù)據(jù)就好了。
讀取并刪除無(wú)用數(shù)據(jù)
f = open("D:/桌面/1960-2019全球GDP數(shù)據(jù).csv","r",encoding="GB2312")
data_lines = f.readlines()
f.close()
data_lines.pop(0)
GB2312 編碼是中文編碼格式文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-596855.html
將數(shù)據(jù)轉(zhuǎn)換為字典
data_dict = {}
for line in data_lines:
data_list = line.split(",") # 每一行以逗號(hào)分割,返回一個(gè)列表
year = data_list[0]
country = data_list[1]
GDP = float(data_list[2][:-1]) # 每一行最后有一個(gè)換行符
# 這里需要做出異常判斷,因?yàn)楫?dāng)我們第一次插入數(shù)據(jù)的時(shí)候并沒(méi)有容器來(lái)裝這些數(shù)據(jù)
try:
data_dict[year].append((country, GDP))
except:
data_dict[year] = []
data_dict[year].append([country, GDP])
創(chuàng)建柱狀圖并添加到時(shí)間線(xiàn)中
sorted_year_line = sorted(data_dict.keys()) # 按時(shí)間順序排序
timeline = Timeline({"scheme":ThemeType.LIGHT}) # 在創(chuàng)建時(shí)間線(xiàn)的時(shí)候傳入scheme參數(shù)可以設(shè)置時(shí)間線(xiàn)的主題,也就是柱狀圖的顏色
for year in sorted_year_line:
x_data = []
y_data = []
data_dict[year].sort(key=lambda element : element[1],reverse=True)
year_data = data_dict[year][0:8] # 取GDP前八的數(shù)據(jù)
for data in year_data:
x_data.append(data[0])
y_data.append(data[1] / 100000000)
bar = Bar()
x_data.reverse()
y_data.reverse() # 讓GDP排名第一的數(shù)據(jù)在最上面,所以我們將x_data 和 y_data中的數(shù)據(jù)反轉(zhuǎn)一下
bar.add_xaxis(x_data)
bar.add_yaxis("GDP(億)",y_data,label_opts=LabelOpts(position="right"))
bar.reversal_axis() # 將x軸和y軸翻轉(zhuǎn)
bar.set_global_opts(
title_opts=TitleOpts(title=f"{year}年全國(guó)GDP數(shù)據(jù)前八")
)
timeline.add(bar,year)
配置選項(xiàng)并生成帶有數(shù)據(jù)的折線(xiàn)圖
timeline.add_schema(
play_interval=1000,
is_timeline_show=True,
is_auto_play=True,
is_loop_play=False
)
timeline.render("1960-2014年全國(guó)GDP數(shù)據(jù)前八.html")
文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-596855.html
到了這里,關(guān)于數(shù)據(jù)可視化——繪制帶有時(shí)間線(xiàn)的柱狀圖的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!