Python?pyecharts?數(shù)據(jù)可視化模塊的配置方法
1. pyecharts 模塊介紹
Echarts 是一個(gè)由百度開源的數(shù)據(jù)可視化,憑借著良好的交互性,精巧的圖表設(shè)計(jì),得到了眾多開發(fā)者的認(rèn)可。而 Python 是一門富有表達(dá)力的語言,很適合用于數(shù)據(jù)處理。當(dāng)數(shù)據(jù)分析遇上數(shù)據(jù)可視化時(shí),pyecharts 誕生了。
pyecharts 官網(wǎng):https://pyecharts.org/#/zh-cn/
pyecharts 畫廊地址:https://gallery.pyecharts.org/#/README
2. pyecharts 模塊安裝
pip install pyecharts
3. pyecharts 配置選項(xiàng)
pyecharts 模塊中有很多配置選項(xiàng),常用到兩個(gè)類別的選項(xiàng):全局配置選項(xiàng)和系列配置選項(xiàng)。
3.1 全局配置選項(xiàng)
全局配置選項(xiàng)可以通過 set_global_opts 方法來進(jìn)行配置,通常對(duì)圖表的一些通用的基礎(chǔ)的元素進(jìn)行配置,例如標(biāo)題、圖例、工具箱、鼠標(biāo)移動(dòng)效果等等,它們與圖表的類型無關(guān)。


示例代碼:通過折線圖對(duì)象對(duì)折線圖進(jìn)行全局配置
from pyecharts.charts import Line
from pyecharts.options import TitleOpts, LegendOpts, ToolboxOpts, VisualMapOpts
# 獲取折線圖對(duì)象
line = Line()
# 對(duì)折線圖進(jìn)行全局配置
line.set_global_opts(
# 設(shè)置標(biāo)題、標(biāo)題的位置...
title_opts=TitleOpts("國家GDP展示", pos_left="center", pos_bottom="1%"),
# 設(shè)置圖例是展示的...
legend_opts=LegendOpts(is_show=True),
# 設(shè)置工具箱是展示的
toolbox_opts=ToolboxOpts(is_show=True),
# 設(shè)置視覺映射是展示的
visualmap_opts=VisualMapOpts(is_show=True)
)
3.2 系列配置選項(xiàng)
系列配置選項(xiàng)是針對(duì)某個(gè)具體的參數(shù)進(jìn)行配置,可以去 pyecharts 官網(wǎng)進(jìn)行了解。

4. 基礎(chǔ)折線圖的構(gòu)建
4.1 基本使用流程
1.導(dǎo)包,導(dǎo)入 Line 功能構(gòu)建折線圖對(duì)象
from pyecharts.charts import Line from pyecharts.options import TitleOpts, LegendOpts, ToolboxOpts, VisualMapOpts
2.獲取折線圖對(duì)象
line = Line()
3.添加 x、y 軸數(shù)據(jù)(添加系列配置)
line.add_xaxis(["中國", "美國", "英國"])
line.add_yaxis("GDP", [30, 20, 10])
4.添加全局配置
line.set_global_opts(
# 設(shè)置標(biāo)題、標(biāo)題的位置...
title_opts=TitleOpts("國家GDP展示", pos_left="center", pos_bottom="1%"),
# 設(shè)置圖例是展示的...
legend_opts=LegendOpts(is_show=True),
# 設(shè)置工具箱是展示的
toolbox_opts=ToolboxOpts(is_show=True),
# 設(shè)置視覺映射是展示的
visualmap_opts=VisualMapOpts(is_show=True)
)
5.生成圖表(通過 render 方法將代碼生成圖像)
line.render()

4.2 實(shí)現(xiàn)2020年美印日確診人數(shù)對(duì)比折線圖
import json
from pyecharts.charts import Line
# 獲取不同國家疫情時(shí)間
from pyecharts.options import TitleOpts, LabelOpts
def getdata(file):
# 處理數(shù)據(jù)
try:
f = open(file, 'r', encoding='utf8')
except FileNotFoundError as e:
print(f"文件不存在,具體錯(cuò)誤為:{e}")
else:
data = f.read()
# JSON 轉(zhuǎn) Python 字典
dict = json.loads(data)
# 獲取 trend
trend_data = dict['data'][0]['trend']
# 獲取日期數(shù)據(jù),用于 x 軸(只拿2020年的數(shù)據(jù))
x_data = trend_data['updateDate'][:314]
# 獲取確認(rèn)數(shù)據(jù),用于 y 軸
y_data = trend_data['list'][0]['data'][:314]
# 返回結(jié)果
return x_data, y_data
finally:
f.close()
# 獲取美國數(shù)據(jù)
us_x_data, us_y_data = getdata("E:\\折線圖數(shù)據(jù)\\美國.txt")
# 獲取印度數(shù)據(jù)
in_x_data, in_y_data = getdata("E:\\折線圖數(shù)據(jù)\\印度.txt")
# 獲取日本數(shù)據(jù)
jp_x_data, jp_y_data = getdata("E:\\折線圖數(shù)據(jù)\\日本.txt")
# 生成圖表
line = Line()
# 添加 x 軸數(shù)據(jù)(日期,公用數(shù)據(jù),不同國家都一樣)
line.add_xaxis(us_x_data)
# 添加 y 軸數(shù)據(jù)(設(shè)置 y 軸的系列配置,將標(biāo)簽不顯示)
line.add_yaxis("美國確診人數(shù)", us_y_data, label_opts=LabelOpts(is_show=False)) # 添加美國數(shù)據(jù)
line.add_yaxis("印度確診人數(shù)", in_y_data, label_opts=LabelOpts(is_show=False)) # 添加印度數(shù)據(jù)
line.add_yaxis("日本確診人數(shù)", jp_y_data, label_opts=LabelOpts(is_show=False)) # 添加日本數(shù)據(jù)
# 配置全局選項(xiàng)
line.set_global_opts(
# 設(shè)置標(biāo)題
title_opts=TitleOpts("2020年美日印三國確診人數(shù)對(duì)比折線圖", pos_left="center", pos_bottom="1%"),
)
# 生成圖表
line.render()

5. 基礎(chǔ)地圖構(gòu)建
5.1 基本使用流程
1.導(dǎo)包,導(dǎo)入 Map 功能獲取地圖對(duì)象
from pyecharts.charts import Map from pyecharts.options import VisualMapOpts
2.獲取地圖對(duì)象
map = Map()
3.準(zhǔn)備好數(shù)據(jù)
data = [
("北京", 99),
("上海", 199),
("廣州", 299),
("湖南", 199),
("安徽", 99),
("湖北", 399),
]
4.添加數(shù)據(jù)到地圖對(duì)象中
# 地圖名稱、傳入的數(shù)據(jù)、地圖類型(默認(rèn)是中國地圖)
map,add("地圖", data, "china")
5.添加全局配置
map.set_global_opts(
# 設(shè)置視覺映射配置
visualmap_opts=VisualMapOpts(
# 打開視覺映射(可能不精準(zhǔn),因此可以開啟手動(dòng)校準(zhǔn))
is_show=True,
# 開啟手動(dòng)校準(zhǔn)范圍
is_piecewise=True,
# 設(shè)置要校準(zhǔn)參數(shù)的具體范圍
pieces=[
{"min": 1, "max": 9, "label": "1~9人", "color": "#CCFFFF"},
{"min": 10, "max": 99, "label": "10~99人", "color": "#FFFF99"},
{"min": 100, "max": 199, "label": "100~199人", "color": "#FF9966"},
{"min": 200, "max": 299, "label": "200~299人", "color": "#FF6666"},
{"min": 300, "label": "300人以上", "color": "#CC3333"},
]
)
)
6.生成地圖
map.render()

5.2 實(shí)現(xiàn)國內(nèi)疫情地圖
import json
from pyecharts.charts import Map
from pyecharts.options import VisualMapOpts, TitleOpts, LegendOpts
# 讀取數(shù)據(jù)
f = open("E:\\地圖數(shù)據(jù)\\疫情.txt", 'r', encoding='utf8')
str_json = f.read()
# 關(guān)閉文件
f.close()
# JSON 轉(zhuǎn) python 字典
data_dict = json.loads(str_json)
# 取到各省數(shù)據(jù)
province_data_list = data_dict['areaTree'][0]['children']
# 組裝每個(gè)省份和確診人數(shù)為元組,并封裝到列表內(nèi)
data_list = []
for province_data in province_data_list:
province_name = province_data['name']
province_total_confirm = province_data['total']['confirm']
data_list.append((province_name, province_total_confirm))
# 創(chuàng)建地圖對(duì)象
map = Map()
# 添加數(shù)據(jù)
map.add("各省確診總?cè)藬?shù)", data_list, "china")
# 設(shè)置全局配置,定制分段的視覺映射
map.set_global_opts(
title_opts=TitleOpts('全國疫情地圖', pos_left='center', pos_bottom='1%'),
legend_opts=LegendOpts(is_show=True),
visualmap_opts=VisualMapOpts(
is_show=True,
is_piecewise=True,
pieces=[
{"min": 1, "max": 9, "label": "1~9人", "color": "#CCFFFF"},
{"min": 10, "max": 99, "label": "10~99人", "color": "#FFFF99"},
{"min": 100, "max": 499, "label": "100~499人", "color": "#FF9966"},
{"min": 500, "max": 999, "label": "500~999人", "color": "#FF6666"},
{"min": 1000, "max": 9999, "label": "1000~9999人", "color": "#CC3333"},
{"min": 10000, "label": "10000人以上", "color": "#990033"}
]
)
)
# 繪圖
map.render()

5.3 實(shí)現(xiàn)省級(jí)疫情地圖
import json
from pyecharts.charts import Map
from pyecharts.options import VisualMapOpts, TitleOpts, LegendOpts
# 讀取數(shù)據(jù)
f = open("E:\\地圖數(shù)據(jù)\\疫情.txt", 'r', encoding='utf8')
str_json = f.read()
# 關(guān)閉文件
f.close()
# JSON 轉(zhuǎn) python 字典
data_dict = json.loads(str_json)
# 取到河南省數(shù)據(jù)
city_data_list = data_dict['areaTree'][0]['children'][3]['children']
# 組裝每個(gè)市和確診人數(shù)為元組,并封裝到列表內(nèi)
data_list = []
for city_data in city_data_list:
city_name = city_data['name'] + "市"
city_total_confirm = city_data['total']['confirm']
data_list.append((city_name, city_total_confirm))
# 創(chuàng)建地圖對(duì)象
map = Map()
# 添加數(shù)據(jù)
map.add("各市確診總?cè)藬?shù)", data_list, "河南")
# 設(shè)置全局配置,定制分段的視覺映射
map.set_global_opts(
title_opts=TitleOpts('河南省疫情地圖', pos_left='center', pos_bottom='1%'),
legend_opts=LegendOpts(is_show=True),
visualmap_opts=VisualMapOpts(
is_show=True,
is_piecewise=True,
pieces=[
{"min": 1, "max": 9, "label": "1~9人", "color": "#CCFFFF"},
{"min": 10, "max": 99, "label": "10~99人", "color": "#FFFF99"},
{"min": 100, "max": 499, "label": "100~499人", "color": "#FF9966"},
{"min": 500, "max": 999, "label": "500~999人", "color": "#FF6666"},
{"min": 1000, "max": 9999, "label": "1000~9999人", "color": "#CC3333"},
{"min": 10000, "label": "10000人以上", "color": "#990033"}
]
)
)
# 繪圖
map.render()

6. 基礎(chǔ)柱狀圖構(gòu)建
6.1 基本使用流程
1.導(dǎo)包,導(dǎo)入 Bar 功能獲取地圖對(duì)象
from pyecharts.charts import Bar from pyecharts.options import *
2.獲取地圖對(duì)象
bar = Bar()
3.添加 x 和 y 軸數(shù)據(jù)
# 添加 x 軸數(shù)據(jù)
bar.add_xaxis(["中國", "英國", "美國"])
# 添加 y 軸數(shù)據(jù)
bar.add_yaxis("GDP", [30, 20, 10])
4.添加全局配置
bar.set_global_opts(
title_opts=TitleOpts("基礎(chǔ)柱狀圖", pos_left='center', pos_bottom='1%')
)
5.生成地圖
bar.render()

6.反轉(zhuǎn) xy 軸
bar.reversal_axis()

7.將數(shù)值標(biāo)簽添設(shè)置到右側(cè)
bar.add_yaxis("GDP", [30, 20, 10], label_opts=LabelOpts(position='right'))

6.2 基礎(chǔ)時(shí)間線柱狀圖
柱狀圖描述的是分類數(shù)據(jù),但很難動(dòng)態(tài)的描述一個(gè)趨勢性的數(shù)據(jù),為此 pyecharts 中提供了一種解決方案時(shí)間線。
如果說一個(gè) Bar、Line 對(duì)象是一張圖表的話,時(shí)間線就是創(chuàng)建一個(gè)一維的 x 軸,軸上的每一個(gè)點(diǎn)就是一個(gè)圖表對(duì)象。
創(chuàng)建時(shí)間線的基礎(chǔ)流程:
1.導(dǎo)包,導(dǎo)入時(shí)間線 Timeline
from pyecharts.charts import Bar, Timeline from pyecharts.options import *
2.準(zhǔn)備好圖表對(duì)象并添加好數(shù)據(jù)
bar1 = 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, 20, 30], label_opts=LabelOpts(position='right'))
bar2.reversal_axis()
bar3 = Bar()
bar3.add_xaxis(["中國", "英國", "美國"])
bar3.add_yaxis("GDP", [60, 30, 40], label_opts=LabelOpts(position='right'))
bar3.reversal_axis()
3.創(chuàng)建時(shí)間線對(duì)象 Timeline
timeline = Timeline()
4.將圖表添加到 Timeline 對(duì)象中
# 添加圖表到時(shí)間線中(圖表對(duì)象,點(diǎn)名稱) timeline.add(bar1, "2020年GDP") timeline.add(bar2, "2021年GDP") timeline.add(bar3, "2022年GDP")
5.通過時(shí)間線繪圖
timeline.render()

6.設(shè)置自動(dòng)播放
timeline.add_schema(
play_interval=1000, # 自動(dòng)播放的時(shí)間間隔,單位毫秒
is_timeline_show=True, # 是否顯示自動(dòng)播放的時(shí)候,顯示時(shí)間線(默認(rèn) True)
is_auto_play=True, # 是否在自動(dòng)播放(默認(rèn) False)
is_loop_play=True # 是否循環(huán)自動(dòng)播放(默認(rèn) True)
)
7.設(shè)置時(shí)間線主題
# 導(dǎo)入 ThemeType
from pyecharts.globals import ThemeType
# 創(chuàng)建時(shí)間線對(duì)象時(shí),設(shè)置主題參數(shù)
timeline = Timeline({"theme": ThemeType.DARK})

主題參數(shù)如下:

6.3 實(shí)現(xiàn)動(dòng)態(tài) GDP 柱狀圖
import json
from pyecharts.charts import Bar, Timeline
from pyecharts.options import *
from pyecharts.globals import ThemeType
# 讀取數(shù)據(jù)
f = open("E:\\動(dòng)態(tài)柱狀圖數(shù)據(jù)\\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)化為字典才能出,格式為 {年份1: [[國家1, GDP], [國家2, GDP]], 年份2: [國家, GDP], ...}
data_dict = dict()
for line in data_lines:
year = int(line.split(',')[0]) # 年份
country = line.split(',')[1] # 國家
gdp = float(line.split(',')[2]) # gdp 數(shù)據(jù),通過 float 強(qiáng)制轉(zhuǎn)換可以把帶有科學(xué)計(jì)數(shù)法的數(shù)字轉(zhuǎn)換為普通數(shù)字
try: # 如果 key 不存在,則會(huì)拋出異常 KeyError
data_dict[year].append([country, gdp])
except KeyError:
data_dict[year] = [[country, gdp]]
# 排序年份(字典對(duì)象的 key 可能是無序的)
sorted_year_list = sorted(data_dict.keys())
# 創(chuàng)建時(shí)間線對(duì)象
timeline = Timeline({"theme": ThemeType.LIGHT})
# 組裝數(shù)據(jù)到 Bar 對(duì)象中,并添加到 timeline 中
for year in sorted_year_list:
data_dict[year].sort(key=lambda element: element[1], reverse=True)
# 該年份GDP前八的國家
year_data = data_dict[year][:8]
x_data = []
y_data = []
for country_gdp in year_data:
x_data.append(country_gdp[0])
y_data.append(country_gdp[1] / 100000000)
# 創(chuàng)建柱狀圖
bar = Bar()
x_data.reverse()
y_data.reverse()
# 添加 x y 軸數(shù)據(jù)
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(f"{year}年GDP全球前8國家", pos_left='5%')
)
# 將 bar 對(duì)象添加到 timeline 中
timeline.add(bar, year)
# 設(shè)置自動(dòng)播放參數(shù)
timeline.add_schema(
play_interval=1000, # 自動(dòng)播放的時(shí)間間隔,單位毫秒
is_timeline_show=True, # 是否顯示自動(dòng)播放的時(shí)候,顯示時(shí)間線(默認(rèn) True)
is_auto_play=True, # 是否在自動(dòng)播放(默認(rèn) False)
is_loop_play=True # 是否循環(huán)自動(dòng)播放(默認(rèn) True)
)
# 通過時(shí)間線繪圖
timeline.render("1960~2019全球GDP前8國家.html")

到此這篇關(guān)于Python pyecharts 數(shù)據(jù)可視化模塊的文章就介紹到這了,更多相關(guān)Python pyecharts 數(shù)據(jù)可視化內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python實(shí)現(xiàn)合并兩個(gè)字典的8種方法
Python有多種方法可以通過使用各種函數(shù)和構(gòu)造函數(shù)來合并字典,本文主要介紹了Python實(shí)現(xiàn)合并兩個(gè)字典的8種方法,具有一定的參考價(jià)值,感興趣的可以了解一下2024-07-07
利用Python裁切tiff圖像且讀取tiff,shp文件的實(shí)例
這篇文章主要介紹了利用Python裁切tiff圖像且讀取tiff,shp文件的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-03-03
pandas實(shí)現(xiàn)數(shù)據(jù)可視化的示例代碼
本文主要介紹了pandas實(shí)現(xiàn)數(shù)據(jù)可視化的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05
Python中for循環(huán)語句實(shí)戰(zhàn)案例
這篇文章主要給大家介紹了關(guān)于Python中for循環(huán)語句的相關(guān)資料,python中for循環(huán)一般用來迭代字符串,列表,元組等,當(dāng)for循環(huán)用于迭代時(shí)不需要考慮循環(huán)次數(shù),循環(huán)次數(shù)由后面的對(duì)象長度來決定,需要的朋友可以參考下2023-09-09
pandas DataFrame 根據(jù)多列的值做判斷,生成新的列值實(shí)例
今天小編就為大家分享一篇pandas DataFrame 根據(jù)多列的值做判斷,生成新的列值實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-05-05

