Python實現(xiàn)動態(tài)柱狀圖的繪制
一.基礎(chǔ)柱狀圖
如圖

演示
from pyecharts.charts import Bar
from pyecharts.options import *
#構(gòu)建柱狀圖對象
bar=Bar()
#添加x軸對象
bar.add_xaxis(["中國","美國","日本"])
#添加y軸對象
bar.add_yaxis("GDP",[40,50,30])
#設(shè)置全局選項
bar.set_global_opts(
title_opts=TitleOpts(title="GDP柱狀圖"),#加名稱
visualmap_opts=VisualMapOpts(#加范圍顯示
is_show=True
)
)
#繪圖
bar.render("基礎(chǔ)柱狀圖.html")結(jié)果是


反轉(zhuǎn)x軸,y軸,設(shè)置數(shù)值標(biāo)簽在右側(cè)
#添加y軸對象
bar.add_yaxis("GDP",[40,50,30],label_opts=LabelOpts(position="right"))
#反轉(zhuǎn)x軸y軸
bar.reversal_axis()結(jié)果是

小結(jié)
- 通過Bar()構(gòu)建一個柱狀圖對象
- 和折線圖一樣,通過add_xaxis()和add_yaxis()添加x和y軸數(shù)據(jù)
- 通過柱狀圖對象的: reversal_axis(),反轉(zhuǎn)x和y軸
- 通過label_opts=LabelOpts(position="right")設(shè)置數(shù)值標(biāo)簽在右側(cè)顯示
二.基礎(chǔ)時間線柱狀圖

演示
from pyecharts.charts import Bar,Timeline
from pyecharts.options import *
from pyecharts.globals import ThemeType
#構(gòu)建柱狀圖對象
bar1=Bar()
#添加x軸對象
bar1.add_xaxis(["中國","美國","日本"])
#添加y軸對象
bar1.add_yaxis("GDP",[40,50,30],label_opts=LabelOpts(position="right"))
#反轉(zhuǎn)x軸y軸
bar1.reversal_axis()
#設(shè)置全局選項
bar1.set_global_opts(
title_opts=TitleOpts(title="GDP柱狀圖"),#加名稱
visualmap_opts=VisualMapOpts(#加范圍顯示
is_show=True
)
)
#構(gòu)建柱狀圖對象
bar2=Bar()
#添加x軸對象
bar2.add_xaxis(["中國","美國","日本"])
#添加y軸對象
bar2.add_yaxis("GDP",[50,60,45],label_opts=LabelOpts(position="right"))
#反轉(zhuǎn)x軸y軸
bar2.reversal_axis()
#設(shè)置全局選項
bar2.set_global_opts(
title_opts=TitleOpts(title="GDP柱狀圖"),#加名稱
visualmap_opts=VisualMapOpts(#加范圍顯示
is_show=True
)
)
#構(gòu)建柱狀圖對象
bar3=Bar()
#添加x軸對象
bar3.add_xaxis(["中國","美國","日本"])
#添加y軸對象
bar3.add_yaxis("GDP",[60,65,40],label_opts=LabelOpts(position="right"))
#反轉(zhuǎn)x軸y軸
bar3.reversal_axis()
#設(shè)置全局選項
bar3.set_global_opts(
title_opts=TitleOpts(title="GDP柱狀圖"),#加名稱
visualmap_opts=VisualMapOpts(#加范圍顯示
is_show=True
)
)
#構(gòu)建時間線對象
timeline=Timeline({"theme":ThemeType.LIGHT})
#在時間線上添加柱狀圖對象
timeline.add(bar1,"點1")
timeline.add(bar2,"點2")
timeline.add(bar3,"點3")
#繪圖是用時間線對象繪圖,而不是用 bar了
timeline.render("基礎(chǔ)時間線柱狀圖.html")結(jié)果是



點擊下面不同的點,就會顯示出不同的柱狀圖
當(dāng)然,這不是我們想要的最終結(jié)果,我們希望他可以制動播放并且一直循環(huán)下去
代碼
#自動播放設(shè)置
timeline.add_schema(
play_interval=1000,#這里是放下一張圖的時間間隔,毫秒為單位
is_timeline_show=True,#是否顯示時間線
is_auto_play=True,#是否自動播放
is_loop_play=True#是否循環(huán)播放
)
#繪圖是用時間線對象繪圖,而不是用 bar了
timeline.render("基礎(chǔ)時間線柱狀圖.html")結(jié)果是

這樣我們就可以控制上圖中的“播放鍵”去控制是否自動循環(huán)播放
三.GDP動態(tài)柱狀圖繪制
1.了解列表的sort方法并配合lambda匿名函數(shù)完成列表排序
在前面我們學(xué)習(xí)過sorted函數(shù),可以對數(shù)據(jù)容器進行排序。
在后面的數(shù)據(jù)處理中,我們需要對列表進行排序,并指定排序規(guī)則,sorted函數(shù)就無法完成了。我們補充學(xué)習(xí)列表的sort方法。
使用方式:
列表.sort(key=選擇排序依據(jù)的函數(shù), reverse=TruelFalse)
- 參數(shù)key,是要求傳入一個函數(shù),表示將列表的每一個元素都傳入函數(shù)中,返回排序的依據(jù)
- 參數(shù)reverse,是否反轉(zhuǎn)排序結(jié)果,True表示降序,F(xiàn)alse表示升序
演示
my_list=[["a",12],["b",4],["c",45]]
def choose_sort_key(element):
return element[1]
my_list.sort(key=choose_sort_key,reverse=True)
print(my_list)
my_list.sort(key=lambda element:element[1],reverse=False)
print(my_list)結(jié)果是

上圖中,第一種是基于帶名函數(shù),第二種是基于lambda函數(shù)
2.完成圖表所需數(shù)據(jù)


#將數(shù)據(jù)轉(zhuǎn)為字典存儲,格式為:
#{ 年份:[[國家, gdp],[國家, gdp],...... ],年份:[[國家,gdp],[國家, gdp],.....], ...... },比如:
#{ 1960:[[美國, 123],[中國, 231],...... ],1961:[[美國,124],[中國, 234],.....], ...... }
#先定義一個字典
data_dict={}
for line in data_lines:
year=int(line.split(",")[0])#年份
country=line.split(",")[1]#省份
GDP=float(line.split(",")[2])#因為美國的GDP是科學(xué)計數(shù)法,所以用float強制轉(zhuǎn)回來
#如何判斷字典里面有沒有制定的key呢?
try:
data_dict[year].append([country,GDP])
except KeyError:
data_dict[year]=[]
data_dict[year].append([country,GDP])
print(data_dict)結(jié)果是


上圖就是1999年的數(shù)據(jù)
3.完成GDP動態(tài)圖表繪制

from pyecharts.charts import Bar,Timeline
from pyecharts.options import *
#讀取文件
f=open("D:/1960-2019全球GDP數(shù)據(jù).csv","r",encoding="ANSI")
data_lines=f.readlines()
#關(guān)閉文件
f.close()
#刪除第一行數(shù)據(jù)
data_lines.pop(0)
#將數(shù)據(jù)轉(zhuǎn)為字典存儲,格式為:
#{ 年份:[[國家, gdp],[國家, gdp],...... ],年份:[[國家,gdp],[國家, gdp],.....], ...... },比如:
#{ 1960:[[美國, 123],[中國, 231],...... ],1961:[[美國,124],[中國, 234],.....], ...... }
#先定義一個字典
data_dict={}
for line in data_lines:
year=int(line.split(",")[0])#年份
country=line.split(",")[1]#省份
GDP=float(line.split(",")[2])#因為美國的GDP是科學(xué)計數(shù)法,所以用float強制轉(zhuǎn)回來
#如何判斷字典里面有沒有制定的key呢?
try:
data_dict[year].append([country,GDP])
except KeyError:
data_dict[year]=[]
data_dict[year].append([country,GDP])
#排序年份
sorted_year_list=sorted(data_dict.keys())
timeline = Timeline()#創(chuàng)建時間線對象
for year in sorted_year_list:
data_dict[year].sort(key=lambda element:element[1],reverse=True)
#取出本年份前八名的國家
year_data=data_dict[year][: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數(shù)據(jù),單位為億元
#構(gòu)建柱狀圖
bar=Bar()
bar.add_xaxis(x_data)
bar.add_yaxis("GDP億元",y_data,label_opts=LabelOpts(position="right"))
#反轉(zhuǎn)x軸,y軸
bar.reversal_axis()
#創(chuàng)建時間線對象
timeline.add(bar,str(year))
#設(shè)置時間為自動播放
timeline.add_schema(
play_interval=1000,#時間間隔
is_timeline_show=True,#是否顯示時間
is_loop_play=True,#是否循環(huán)
is_auto_play=True#是否自動播放
)
#繪圖
timeline.render("1960——2019全球GDP前八國家.html")結(jié)果是

存在問題:我們希望第一行是GDP數(shù)量最多的,然后依次遞減
解決方法很簡單:把x軸和y軸上的數(shù)據(jù)都反轉(zhuǎn)一下就可以了
#構(gòu)建柱狀圖
bar=Bar()
x_data.reverse()
y_data.reverse()
bar.add_xaxis(x_data)
bar.add_yaxis("GDP億元",y_data,label_opts=LabelOpts(position="right"))結(jié)果是

這樣就基本沒問題了
我們還可以給他:
添加主題類型
from pyecharts.globals import ThemeType
timeline = Timeline({"theme":ThemeType.LIGHT})#創(chuàng)建時間線對象結(jié)果是

設(shè)置動態(tài)標(biāo)題
#反轉(zhuǎn)x軸,y軸
bar.reversal_axis()
#設(shè)置每一年的圖表標(biāo)題
bar.set_global_opts(
title_opts=TitleOpts(title=f"{year}年全球GDP前八數(shù)據(jù)")
)結(jié)果是

四.完整代碼
from pyecharts.charts import Bar,Timeline
from pyecharts.options import *
from pyecharts.globals import ThemeType
#讀取文件
f=open("D:/1960-2019全球GDP數(shù)據(jù).csv","r",encoding="ANSI")
data_lines=f.readlines()
#關(guān)閉文件
f.close()
#刪除第一行數(shù)據(jù)
data_lines.pop(0)
#將數(shù)據(jù)轉(zhuǎn)為字典存儲,格式為:
#{ 年份:[[國家, gdp],[國家, gdp],...... ],年份:[[國家,gdp],[國家, gdp],.....], ...... },比如:
#{ 1960:[[美國, 123],[中國, 231],...... ],1961:[[美國,124],[中國, 234],.....], ...... }
#先定義一個字典
data_dict={}
for line in data_lines:
year=int(line.split(",")[0])#年份
country=line.split(",")[1]#省份
GDP=float(line.split(",")[2])#因為美國的GDP是科學(xué)計數(shù)法,所以用float強制轉(zhuǎn)回來
#如何判斷字典里面有沒有制定的key呢?
try:
data_dict[year].append([country,GDP])
except KeyError:
data_dict[year]=[]
data_dict[year].append([country,GDP])
#排序年份
sorted_year_list=sorted(data_dict.keys())
timeline = Timeline({"theme":ThemeType.LIGHT})#創(chuàng)建時間線對象
for year in sorted_year_list:
data_dict[year].sort(key=lambda element:element[1],reverse=True)
#取出本年份前八名的國家
year_data=data_dict[year][: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數(shù)據(jù),單位為億元
#構(gòu)建柱狀圖
bar=Bar()
x_data.reverse()
y_data.reverse()
bar.add_xaxis(x_data)
bar.add_yaxis("GDP億元",y_data,label_opts=LabelOpts(position="right"))
#反轉(zhuǎn)x軸,y軸
bar.reversal_axis()
#設(shè)置每一年的圖表標(biāo)題
bar.set_global_opts(
title_opts=TitleOpts(title=f"{year}年全球GDP前八數(shù)據(jù)")
)
#創(chuàng)建時間線對象
timeline.add(bar,str(year))
#設(shè)置時間為自動播放
timeline.add_schema(
play_interval=1000,#時間間隔
is_timeline_show=True,#是否顯示時間
is_loop_play=True,#是否循環(huán)
is_auto_play=True#是否自動播放
)
#繪圖
timeline.render("1960——2019全球GDP前八國家.html")以上就是Python實現(xiàn)動態(tài)柱狀圖的繪制的詳細內(nèi)容,更多關(guān)于Python動態(tài)柱狀圖的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
如何利用python批量提取txt文本中所需文本并寫入excel
最近幫人寫了幾個小程序,所以記錄下,下面這篇文章主要給大家介紹了關(guān)于如何利用python批量提取txt文本中所需文本并寫入excel的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2022-07-07
tensorboard實現(xiàn)同時顯示訓(xùn)練曲線和測試曲線
今天小編就為大家分享一篇tensorboard實現(xiàn)同時顯示訓(xùn)練曲線和測試曲線,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01
在Qt中正確的設(shè)置窗體的背景圖片的幾種方法總結(jié)
今天小編就為大家分享一篇在Qt中正確的設(shè)置窗體的背景圖片的幾種方法總結(jié),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-06-06

