Python實(shí)現(xiàn)動(dòng)態(tài)柱狀圖的繪制
一.基礎(chǔ)柱狀圖
如圖
演示
from pyecharts.charts import Bar from pyecharts.options import * #構(gòu)建柱狀圖對(duì)象 bar=Bar() #添加x軸對(duì)象 bar.add_xaxis(["中國(guó)","美國(guó)","日本"]) #添加y軸對(duì)象 bar.add_yaxis("GDP",[40,50,30]) #設(shè)置全局選項(xiàng) bar.set_global_opts( title_opts=TitleOpts(title="GDP柱狀圖"),#加名稱(chēng) visualmap_opts=VisualMapOpts(#加范圍顯示 is_show=True ) ) #繪圖 bar.render("基礎(chǔ)柱狀圖.html")
結(jié)果是
反轉(zhuǎn)x軸,y軸,設(shè)置數(shù)值標(biāo)簽在右側(cè)
#添加y軸對(duì)象 bar.add_yaxis("GDP",[40,50,30],label_opts=LabelOpts(position="right")) #反轉(zhuǎn)x軸y軸 bar.reversal_axis()
結(jié)果是
小結(jié)
- 通過(guò)Bar()構(gòu)建一個(gè)柱狀圖對(duì)象
- 和折線(xiàn)圖一樣,通過(guò)add_xaxis()和add_yaxis()添加x和y軸數(shù)據(jù)
- 通過(guò)柱狀圖對(duì)象的: reversal_axis(),反轉(zhuǎn)x和y軸
- 通過(guò)label_opts=LabelOpts(position="right")設(shè)置數(shù)值標(biāo)簽在右側(cè)顯示
二.基礎(chǔ)時(shí)間線(xiàn)柱狀圖
演示
from pyecharts.charts import Bar,Timeline from pyecharts.options import * from pyecharts.globals import ThemeType #構(gòu)建柱狀圖對(duì)象 bar1=Bar() #添加x軸對(duì)象 bar1.add_xaxis(["中國(guó)","美國(guó)","日本"]) #添加y軸對(duì)象 bar1.add_yaxis("GDP",[40,50,30],label_opts=LabelOpts(position="right")) #反轉(zhuǎn)x軸y軸 bar1.reversal_axis() #設(shè)置全局選項(xiàng) bar1.set_global_opts( title_opts=TitleOpts(title="GDP柱狀圖"),#加名稱(chēng) visualmap_opts=VisualMapOpts(#加范圍顯示 is_show=True ) ) #構(gòu)建柱狀圖對(duì)象 bar2=Bar() #添加x軸對(duì)象 bar2.add_xaxis(["中國(guó)","美國(guó)","日本"]) #添加y軸對(duì)象 bar2.add_yaxis("GDP",[50,60,45],label_opts=LabelOpts(position="right")) #反轉(zhuǎn)x軸y軸 bar2.reversal_axis() #設(shè)置全局選項(xiàng) bar2.set_global_opts( title_opts=TitleOpts(title="GDP柱狀圖"),#加名稱(chēng) visualmap_opts=VisualMapOpts(#加范圍顯示 is_show=True ) ) #構(gòu)建柱狀圖對(duì)象 bar3=Bar() #添加x軸對(duì)象 bar3.add_xaxis(["中國(guó)","美國(guó)","日本"]) #添加y軸對(duì)象 bar3.add_yaxis("GDP",[60,65,40],label_opts=LabelOpts(position="right")) #反轉(zhuǎn)x軸y軸 bar3.reversal_axis() #設(shè)置全局選項(xiàng) bar3.set_global_opts( title_opts=TitleOpts(title="GDP柱狀圖"),#加名稱(chēng) visualmap_opts=VisualMapOpts(#加范圍顯示 is_show=True ) ) #構(gòu)建時(shí)間線(xiàn)對(duì)象 timeline=Timeline({"theme":ThemeType.LIGHT}) #在時(shí)間線(xiàn)上添加柱狀圖對(duì)象 timeline.add(bar1,"點(diǎn)1") timeline.add(bar2,"點(diǎn)2") timeline.add(bar3,"點(diǎn)3") #繪圖是用時(shí)間線(xiàn)對(duì)象繪圖,而不是用 bar了 timeline.render("基礎(chǔ)時(shí)間線(xiàn)柱狀圖.html")
結(jié)果是
點(diǎn)擊下面不同的點(diǎn),就會(huì)顯示出不同的柱狀圖
當(dāng)然,這不是我們想要的最終結(jié)果,我們希望他可以制動(dòng)播放并且一直循環(huán)下去
代碼
#自動(dòng)播放設(shè)置 timeline.add_schema( play_interval=1000,#這里是放下一張圖的時(shí)間間隔,毫秒為單位 is_timeline_show=True,#是否顯示時(shí)間線(xiàn) is_auto_play=True,#是否自動(dòng)播放 is_loop_play=True#是否循環(huán)播放 ) #繪圖是用時(shí)間線(xiàn)對(duì)象繪圖,而不是用 bar了 timeline.render("基礎(chǔ)時(shí)間線(xiàn)柱狀圖.html")
結(jié)果是
這樣我們就可以控制上圖中的“播放鍵”去控制是否自動(dòng)循環(huán)播放
三.GDP動(dòng)態(tài)柱狀圖繪制
1.了解列表的sort方法并配合lambda匿名函數(shù)完成列表排序
在前面我們學(xué)習(xí)過(guò)sorted函數(shù),可以對(duì)數(shù)據(jù)容器進(jìn)行排序。
在后面的數(shù)據(jù)處理中,我們需要對(duì)列表進(jìn)行排序,并指定排序規(guī)則,sorted函數(shù)就無(wú)法完成了。我們補(bǔ)充學(xué)習(xí)列表的sort方法。
使用方式:
列表.sort(key=選擇排序依據(jù)的函數(shù), reverse=TruelFalse)
- 參數(shù)key,是要求傳入一個(gè)函數(shù),表示將列表的每一個(gè)元素都傳入函數(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)為字典存儲(chǔ),格式為: #{ 年份:[[國(guó)家, gdp],[國(guó)家, gdp],...... ],年份:[[國(guó)家,gdp],[國(guó)家, gdp],.....], ...... },比如: #{ 1960:[[美國(guó), 123],[中國(guó), 231],...... ],1961:[[美國(guó),124],[中國(guó), 234],.....], ...... } #先定義一個(gè)字典 data_dict={} for line in data_lines: year=int(line.split(",")[0])#年份 country=line.split(",")[1]#省份 GDP=float(line.split(",")[2])#因?yàn)槊绹?guó)的GDP是科學(xué)計(jì)數(shù)法,所以用float強(qiáng)制轉(zhuǎn)回來(lái) #如何判斷字典里面有沒(méi)有制定的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動(dòng)態(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)為字典存儲(chǔ),格式為: #{ 年份:[[國(guó)家, gdp],[國(guó)家, gdp],...... ],年份:[[國(guó)家,gdp],[國(guó)家, gdp],.....], ...... },比如: #{ 1960:[[美國(guó), 123],[中國(guó), 231],...... ],1961:[[美國(guó),124],[中國(guó), 234],.....], ...... } #先定義一個(gè)字典 data_dict={} for line in data_lines: year=int(line.split(",")[0])#年份 country=line.split(",")[1]#省份 GDP=float(line.split(",")[2])#因?yàn)槊绹?guó)的GDP是科學(xué)計(jì)數(shù)法,所以用float強(qiáng)制轉(zhuǎn)回來(lái) #如何判斷字典里面有沒(méi)有制定的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)建時(shí)間線(xiàn)對(duì)象 for year in sorted_year_list: data_dict[year].sort(key=lambda element:element[1],reverse=True) #取出本年份前八名的國(guó)家 year_data=data_dict[year][:8] x_data=[] y_data=[] for country_gdp in year_data: x_data.append(country_gdp[0])#x軸添加國(guó)家 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)建時(shí)間線(xiàn)對(duì)象 timeline.add(bar,str(year)) #設(shè)置時(shí)間為自動(dòng)播放 timeline.add_schema( play_interval=1000,#時(shí)間間隔 is_timeline_show=True,#是否顯示時(shí)間 is_loop_play=True,#是否循環(huán) is_auto_play=True#是否自動(dòng)播放 ) #繪圖 timeline.render("1960——2019全球GDP前八國(guó)家.html")
結(jié)果是
存在問(wèn)題:我們希望第一行是GDP數(shù)量最多的,然后依次遞減
解決方法很簡(jiǎn)單:把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é)果是
這樣就基本沒(méi)問(wèn)題了
我們還可以給他:
添加主題類(lèi)型
from pyecharts.globals import ThemeType timeline = Timeline({"theme":ThemeType.LIGHT})#創(chuàng)建時(shí)間線(xiàn)對(duì)象
結(jié)果是
設(shè)置動(dòng)態(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)為字典存儲(chǔ),格式為: #{ 年份:[[國(guó)家, gdp],[國(guó)家, gdp],...... ],年份:[[國(guó)家,gdp],[國(guó)家, gdp],.....], ...... },比如: #{ 1960:[[美國(guó), 123],[中國(guó), 231],...... ],1961:[[美國(guó),124],[中國(guó), 234],.....], ...... } #先定義一個(gè)字典 data_dict={} for line in data_lines: year=int(line.split(",")[0])#年份 country=line.split(",")[1]#省份 GDP=float(line.split(",")[2])#因?yàn)槊绹?guó)的GDP是科學(xué)計(jì)數(shù)法,所以用float強(qiáng)制轉(zhuǎn)回來(lái) #如何判斷字典里面有沒(méi)有制定的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)建時(shí)間線(xiàn)對(duì)象 for year in sorted_year_list: data_dict[year].sort(key=lambda element:element[1],reverse=True) #取出本年份前八名的國(guó)家 year_data=data_dict[year][:8] x_data=[] y_data=[] for country_gdp in year_data: x_data.append(country_gdp[0])#x軸添加國(guó)家 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)建時(shí)間線(xiàn)對(duì)象 timeline.add(bar,str(year)) #設(shè)置時(shí)間為自動(dòng)播放 timeline.add_schema( play_interval=1000,#時(shí)間間隔 is_timeline_show=True,#是否顯示時(shí)間 is_loop_play=True,#是否循環(huán) is_auto_play=True#是否自動(dòng)播放 ) #繪圖 timeline.render("1960——2019全球GDP前八國(guó)家.html")
以上就是Python實(shí)現(xiàn)動(dòng)態(tài)柱狀圖的繪制的詳細(xì)內(nèi)容,更多關(guān)于Python動(dòng)態(tài)柱狀圖的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
新版Pycharm顯示Conda?executable?is?not?found解決辦法
這篇文章主要給大家介紹了關(guān)于新版Pycharm顯示Conda?executable?is?not?found解決辦法,文中通過(guò)圖文介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Pycharm具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-07-07python實(shí)現(xiàn)網(wǎng)上購(gòu)物系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)網(wǎng)上購(gòu)物系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06python安裝cxOracle避坑總結(jié)不要直接pip install
這篇文章主要為大家介紹了python安裝cx_Oracle是遇到的一些問(wèn)題的解決辦法的總結(jié),來(lái)幫大家避避坑有需要的朋友可以借鑒參考下,希望能夠有所幫助祝大家多多進(jìn)步2021-10-10python 利用jieba.analyse進(jìn)行 關(guān)鍵詞提取
這篇文章主要介紹了python 利用jieba.analyse進(jìn)行關(guān)鍵詞提取的方法,幫助大家更好的利用python,感興趣的朋友可以了解下2020-12-12Django3.2 自動(dòng)發(fā)現(xiàn)所有路由代碼詳解
這篇文章主要為大家介紹了Django自動(dòng)發(fā)現(xiàn)所有路由的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助2021-12-12