Python?pyecharts?數(shù)據(jù)可視化模塊的配置方法
1. pyecharts 模塊介紹
Echarts 是一個由百度開源的數(shù)據(jù)可視化,憑借著良好的交互性,精巧的圖表設(shè)計,得到了眾多開發(fā)者的認可。而 Python 是一門富有表達力的語言,很適合用于數(shù)據(jù)處理。當(dāng)數(shù)據(jù)分析遇上數(shù)據(jù)可視化時,pyecharts 誕生了。
pyecharts 官網(wǎng):https://pyecharts.org/#/zh-cn/
pyecharts 畫廊地址:https://gallery.pyecharts.org/#/README
2. pyecharts 模塊安裝
pip install pyecharts
3. pyecharts 配置選項
pyecharts 模塊中有很多配置選項,常用到兩個類別的選項:全局配置選項和系列配置選項。
3.1 全局配置選項
全局配置選項可以通過 set_global_opts
方法來進行配置,通常對圖表的一些通用的基礎(chǔ)的元素進行配置,例如標(biāo)題、圖例、工具箱、鼠標(biāo)移動效果等等,它們與圖表的類型無關(guān)。
示例代碼:通過折線圖對象對折線圖進行全局配置
from pyecharts.charts import Line from pyecharts.options import TitleOpts, LegendOpts, ToolboxOpts, VisualMapOpts # 獲取折線圖對象 line = Line() # 對折線圖進行全局配置 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 系列配置選項
系列配置選項是針對某個具體的參數(shù)進行配置,可以去 pyecharts 官網(wǎng)進行了解。
4. 基礎(chǔ)折線圖的構(gòu)建
4.1 基本使用流程
1.導(dǎo)包,導(dǎo)入 Line 功能構(gòu)建折線圖對象
from pyecharts.charts import Line from pyecharts.options import TitleOpts, LegendOpts, ToolboxOpts, VisualMapOpts
2.獲取折線圖對象
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 實現(xiàn)2020年美印日確診人數(shù)對比折線圖
import json from pyecharts.charts import Line # 獲取不同國家疫情時間 from pyecharts.options import TitleOpts, LabelOpts def getdata(file): # 處理數(shù)據(jù) try: f = open(file, 'r', encoding='utf8') except FileNotFoundError as e: print(f"文件不存在,具體錯誤為:{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] # 獲取確認數(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ù) # 配置全局選項 line.set_global_opts( # 設(shè)置標(biāo)題 title_opts=TitleOpts("2020年美日印三國確診人數(shù)對比折線圖", pos_left="center", pos_bottom="1%"), ) # 生成圖表 line.render()
5. 基礎(chǔ)地圖構(gòu)建
5.1 基本使用流程
1.導(dǎo)包,導(dǎo)入 Map 功能獲取地圖對象
from pyecharts.charts import Map from pyecharts.options import VisualMapOpts
2.獲取地圖對象
map = Map()
3.準(zhǔn)備好數(shù)據(jù)
data = [ ("北京", 99), ("上海", 199), ("廣州", 299), ("湖南", 199), ("安徽", 99), ("湖北", 399), ]
4.添加數(shù)據(jù)到地圖對象中
# 地圖名稱、傳入的數(shù)據(jù)、地圖類型(默認是中國地圖) map,add("地圖", data, "china")
5.添加全局配置
map.set_global_opts( # 設(shè)置視覺映射配置 visualmap_opts=VisualMapOpts( # 打開視覺映射(可能不精準(zhǔn),因此可以開啟手動校準(zhǔn)) is_show=True, # 開啟手動校準(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 實現(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'] # 組裝每個省份和確診人數(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)建地圖對象 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 實現(xiàn)省級疫情地圖
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'] # 組裝每個市和確診人數(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)建地圖對象 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 功能獲取地圖對象
from pyecharts.charts import Bar from pyecharts.options import *
2.獲取地圖對象
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ù)據(jù),但很難動態(tài)的描述一個趨勢性的數(shù)據(jù),為此 pyecharts 中提供了一種解決方案時間線。
如果說一個 Bar、Line 對象是一張圖表的話,時間線就是創(chuàng)建一個一維的 x 軸,軸上的每一個點就是一個圖表對象。
創(chuàng)建時間線的基礎(chǔ)流程:
1.導(dǎo)包,導(dǎo)入時間線 Timeline
from pyecharts.charts import Bar, Timeline from pyecharts.options import *
2.準(zhǔn)備好圖表對象并添加好數(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)建時間線對象 Timeline
timeline = Timeline()
4.將圖表添加到 Timeline 對象中
# 添加圖表到時間線中(圖表對象,點名稱) timeline.add(bar1, "2020年GDP") timeline.add(bar2, "2021年GDP") timeline.add(bar3, "2022年GDP")
5.通過時間線繪圖
timeline.render()
6.設(shè)置自動播放
timeline.add_schema( play_interval=1000, # 自動播放的時間間隔,單位毫秒 is_timeline_show=True, # 是否顯示自動播放的時候,顯示時間線(默認 True) is_auto_play=True, # 是否在自動播放(默認 False) is_loop_play=True # 是否循環(huán)自動播放(默認 True) )
7.設(shè)置時間線主題
# 導(dǎo)入 ThemeType from pyecharts.globals import ThemeType # 創(chuàng)建時間線對象時,設(shè)置主題參數(shù) timeline = Timeline({"theme": ThemeType.DARK})
主題參數(shù)如下:
6.3 實現(xiàn)動態(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:\\動態(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 強制轉(zhuǎn)換可以把帶有科學(xué)計數(shù)法的數(shù)字轉(zhuǎn)換為普通數(shù)字 try: # 如果 key 不存在,則會拋出異常 KeyError data_dict[year].append([country, gdp]) except KeyError: data_dict[year] = [[country, gdp]] # 排序年份(字典對象的 key 可能是無序的) sorted_year_list = sorted(data_dict.keys()) # 創(chuàng)建時間線對象 timeline = Timeline({"theme": ThemeType.LIGHT}) # 組裝數(shù)據(jù)到 Bar 對象中,并添加到 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 對象添加到 timeline 中 timeline.add(bar, year) # 設(shè)置自動播放參數(shù) timeline.add_schema( play_interval=1000, # 自動播放的時間間隔,單位毫秒 is_timeline_show=True, # 是否顯示自動播放的時候,顯示時間線(默認 True) is_auto_play=True, # 是否在自動播放(默認 False) is_loop_play=True # 是否循環(huán)自動播放(默認 True) ) # 通過時間線繪圖 timeline.render("1960~2019全球GDP前8國家.html")
到此這篇關(guān)于Python pyecharts 數(shù)據(jù)可視化模塊的文章就介紹到這了,更多相關(guān)Python pyecharts 數(shù)據(jù)可視化內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Kwargs傳遞給Python 中的另一個函數(shù)實現(xiàn)方法
Python 列出了可以傳遞給程序中的函數(shù)的兩種類型的參數(shù), 非關(guān)鍵字參數(shù)**args和關(guān)鍵字參數(shù) **kwargs ,在本文中,我們將討論如何使用關(guān)鍵字參數(shù)及如何將關(guān)鍵字參數(shù)傳遞給另一個函數(shù),感興趣的朋友跟隨小編一起看看吧2023-08-08django之靜態(tài)文件 django 2.0 在網(wǎng)頁中顯示圖片的例子
今天小編就為大家分享一篇django之靜態(tài)文件 django 2.0 在網(wǎng)頁中顯示圖片的例子,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-07-07matplotlib如何設(shè)置坐標(biāo)軸刻度的個數(shù)及標(biāo)簽的方法總結(jié)
這里介紹兩種設(shè)置坐標(biāo)軸刻度的方法,一種是利用pyplot提交的api去進行設(shè)置,另一種是通過調(diào)用面向?qū)ο蟮腶pi, 即通過matplotlib.axes.Axes去設(shè)置,需要的朋友可以參考下2021-06-06Django集成搜索引擎Elasticserach的方法示例
這篇文章主要介紹了Django集成搜索引擎Elasticserach的方法示例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06python 實現(xiàn)的發(fā)送郵件模板【普通郵件、帶附件、帶圖片郵件】
這篇文章主要介紹了python 實現(xiàn)的發(fā)送郵件模板,包含Python發(fā)送普通郵件、帶附件及帶圖片郵件相關(guān)實現(xiàn)技巧,需要的朋友可以參考下2019-07-07新手學(xué)習(xí)Python2和Python3中print不同的用法
在本篇文章里小編給大家分享的是關(guān)于Python2和Python3中print不同的用法,有興趣的朋友們可以學(xué)習(xí)下。2020-06-06Python?matplotlib實戰(zhàn)之氣泡圖繪制
氣泡圖是一種多變量的統(tǒng)計圖表,可以看作是散點圖的變形,這篇文章主要為大家介紹了如何使用Matplotlib繪制氣泡圖,需要的小伙伴可以參考下2023-08-08