微網(wǎng)站和小程序的區(qū)別站長統(tǒng)計app下載大全
終于來到最后一個數(shù)據(jù)可視化的文章拿啦~~~
在這里學(xué)習(xí)如何繪制動態(tài)柱狀圖
我先整個活
(?′?‵?)I L???????
什么是pyecharts?
答:
Python的Pyecharts軟件包。它是一個用于Python數(shù)據(jù)可視化和圖表繪制的庫,可用于制作各種圖表和可視化結(jié)果,包括柱狀圖、折線圖、餅圖、散點圖、地圖等。Pyecharts使用JavaScript的ECharts庫進行底層繪制,因此它提供了易于使用的Python界面和大量的示例代碼。同時,Pyecharts還支持在Jupyter notebook中的實時交互和動態(tài)展示,使其非常適合于數(shù)據(jù)分析和數(shù)據(jù)可視化。
1.案例效果
2.創(chuàng)建基礎(chǔ)柱狀圖?
?(1)通過Bar對象構(gòu)建柱狀圖
(2)反轉(zhuǎn)X,Y軸
?
(3)將數(shù)據(jù)置于右側(cè)?
(4)整體代碼示例
?
"""構(gòu)建基礎(chǔ)柱狀圖
"""
from pyecharts.charts import Bar
from pyecharts.options import LabelOpts# 使用Bar構(gòu)建基礎(chǔ)柱狀圖
bar = Bar()
# 添加X軸
bar.add_xaxis(["中國", "美國", "英國"])
# 添加Y軸
# 設(shè)置數(shù)值標(biāo)簽在右側(cè)
bar.add_yaxis("GDP",[30, 20, 10],label_opts=LabelOpts(position="right"))
# 反轉(zhuǎn)x軸和y軸
bar.reversal_axis()# 繪圖
bar.render("普通柱狀圖.html")
?
3.創(chuàng)建基礎(chǔ)時間柱狀圖
(1)創(chuàng)建時間線
?(2)設(shè)置自動播放
(3)設(shè)置時間線的主題
(4)整體代碼
"""基礎(chǔ)時間柱狀圖
"""
from pyecharts.charts import Bar, Timeline
from pyecharts.options import LabelOpts
from pyecharts.globals import ThemeTypebar1 = Bar()
bar1.add_xaxis(['中國', '美國', '英國'])
bar1.add_yaxis("GDP",[30, 20, 10],label_opts=LabelOpts(position="right"))
bar1.reversal_axis()bar2 = Bar()
bar2.add_xaxis(['中國', '美國', '英國'])
bar2.add_yaxis("GDP",[50, 50, 100],label_opts=LabelOpts(position="right"))
bar2.reversal_axis()bar3 = Bar()
bar3.add_xaxis(['中國', '美國', '英國'])
bar3.add_yaxis("GDP",[1000, 600, 300],label_opts=LabelOpts(position="right"))
bar3.reversal_axis()# 構(gòu)建時間線對象
timeline = Timeline(# 設(shè)置時間線的主題{"theme": ThemeType.LIGHT}
)
# 在時間線內(nèi)添加柱狀圖對象
timeline.add(bar1, "點1")
timeline.add(bar2, "點2")
timeline.add(bar3, "點3")# 設(shè)置自動播放
timeline.add_schema(# 時間間隔play_interval=1000,# 是否顯示時間線is_timeline_show=True,# 是否自動播放is_auto_play=True,# 是否循環(huán)自動播放is_loop_play=True
)# 有了時間線之后繪圖需要使用時間線對象繪圖
timeline.render("基礎(chǔ)時間線柱狀圖.html")
4.GDP動態(tài)柱狀圖的繪制
(1)補充知識:列表的sort方法
sort()是Python中用于排序列表的內(nèi)置函數(shù)。使用sort()方法可以對列表進行升序排列或降序排列。
sort()函數(shù)有兩種用法:
1. 對列表進行升序排列:
my_list = [2, 5, 1, 9, 4]my_list.sort()print(my_list) # 輸出 [1, 2, 4, 5, 9]
2. 對列表進行降序排列:
my_list = [2, 5, 1, 9, 4]my_list.sort(reverse=True)print(my_list) # 輸出 [9, 5, 4, 2, 1]
3.sorted
另外,之前學(xué)習(xí)過sorted()函數(shù)對列表進行排序,不同的是,sorted()函數(shù)不會修改原始列表,而是返回一個新的已排序的列表。
例如:
my_list = [2, 5, 1, 9, 4]
sorted_list = sorted(my_list)
print(my_list) # 輸出 [2, 5, 1, 9, 4]
print(sorted_list) # 輸出 [1, 2, 4, 5, 9]
注:sort()和sorted()函數(shù)都是在原始列表的基礎(chǔ)上進行排序,因此會對原始列表做出修改或返回一個新的已排序的列表。如果不想對原始列表進行修改,可以先對原始列表進行復(fù)制再進行排序。
4.使用方法
(2)數(shù)據(jù)處理
1.需求分析
2.處理數(shù)據(jù)
"""GDP動態(tài)柱狀圖繪制
"""
from pyecharts.charts import Bar, Timeline
from pyecharts.options import *
from pyecharts.globals import ThemeType# 讀取數(shù)據(jù)
f = open("D:\\IOText\\DataDoing\\1960-2019全球GDP數(shù)據(jù).csv", "r", encoding="GB2312")
data_lines = f.readlines()
# 關(guān)閉文件
f.close()
# 刪除第一條數(shù)據(jù)
data_lines.pop(0)
# 將數(shù)據(jù)轉(zhuǎn)化為字典存儲,格式
# 年份: [[國家,gdp],[國家,gdp]]
# 定義字典對象存儲
data_dict = {}
for line in data_lines:year = int(line.split(",")[0])country = line.split(",")[1]GDP = float(line.split(",")[2])# 判斷年份try:data_dict[year].append([country, GDP])except KeyError:data_dict[year] = []data_dict[year].append([country, GDP])# 創(chuàng)建時間線對象
timeline = Timeline({"theme": ThemeType.LIGHT}
)# 排序年份,由小到大
sorted_year_list = sorted(data_dict.keys())
for year in sorted_year_list:# 由高到低排序data_dict[year].sort(key=lambda element: element[1], reverse=True)# 取GDP前八的year_data_before8 = data_dict[year][0:8]x_data = []y_data = []for country_GDP in year_data_before8:# x軸添加國家x_data.append(country_GDP[0])# y軸添加GDPy_data.append(country_GDP[1] / 100000000)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)xy軸bar.reversal_axis()# 設(shè)置每一年的圖標(biāo)的標(biāo)題bar.set_global_opts(title_opts=TitleOpts(title=f"{year}年全球前八GDP"))# 加入時間線timeline.add(bar, str(year))# 設(shè)置
timeline.add_schema(play_interval=1000,is_timeline_show=True,is_auto_play=True,is_loop_play=True
)# 創(chuàng)建圖
timeline.render("1960~2019年全球GDP前八國家.html")
結(jié)語
對于剛剛接觸編程的同學(xué)來說,這個問題肯定是很難得,但是多練習(xí)練習(xí)就好了,ヾ(?°?°?)ノ゙
拜拜ヾ( ̄▽ ̄)Bye~Bye~