Python?pyecharts?數(shù)據(jù)可視化模塊的配置方法
1. pyecharts 模塊介紹
Echarts 是一個(gè)由百度開源的數(shù)據(jù)可視化,憑借著良好的交互性,精巧的圖表設(shè)計(jì),得到了眾多開發(fā)者的認(rèn)可。而 Python 是一門富有表達(dá)力的語(yǔ)言,很適合用于數(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)可以通過(guò) set_global_opts
方法來(lái)進(jìn)行配置,通常對(duì)圖表的一些通用的基礎(chǔ)的元素進(jìn)行配置,例如標(biāo)題、圖例、工具箱、鼠標(biāo)移動(dòng)效果等等,它們與圖表的類型無(wú)關(guān)。
示例代碼:通過(guò)折線圖對(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("國(guó)家GDP展示", pos_left="center", pos_bottom="1%"), # 設(shè)置圖例是展示的... legend_opts=LegendOpts(is_show=True), # 設(shè)置工具箱是展示的 toolbox_opts=ToolboxOpts(is_show=True), # 設(shè)置視覺(jué)映射是展示的 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(["中國(guó)", "美國(guó)", "英國(guó)"]) line.add_yaxis("GDP", [30, 20, 10])
4.添加全局配置
line.set_global_opts( # 設(shè)置標(biāo)題、標(biāo)題的位置... title_opts=TitleOpts("國(guó)家GDP展示", pos_left="center", pos_bottom="1%"), # 設(shè)置圖例是展示的... legend_opts=LegendOpts(is_show=True), # 設(shè)置工具箱是展示的 toolbox_opts=ToolboxOpts(is_show=True), # 設(shè)置視覺(jué)映射是展示的 visualmap_opts=VisualMapOpts(is_show=True) )
5.生成圖表(通過(guò) render 方法將代碼生成圖像)
line.render()
4.2 實(shí)現(xiàn)2020年美印日確診人數(shù)對(duì)比折線圖
import json from pyecharts.charts import Line # 獲取不同國(guó)家疫情時(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() # 獲取美國(guó)數(shù)據(jù) us_x_data, us_y_data = getdata("E:\\折線圖數(shù)據(jù)\\美國(guó).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ù),不同國(guó)家都一樣) line.add_xaxis(us_x_data) # 添加 y 軸數(shù)據(jù)(設(shè)置 y 軸的系列配置,將標(biāo)簽不顯示) line.add_yaxis("美國(guó)確診人數(shù)", us_y_data, label_opts=LabelOpts(is_show=False)) # 添加美國(guó)數(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年美日印三國(guó)確診人數(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)是中國(guó)地圖) map,add("地圖", data, "china")
5.添加全局配置
map.set_global_opts( # 設(shè)置視覺(jué)映射配置 visualmap_opts=VisualMapOpts( # 打開視覺(jué)映射(可能不精準(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)國(guó)內(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è)置全局配置,定制分段的視覺(jué)映射 map.set_global_opts( title_opts=TitleOpts('全國(guó)疫情地圖', 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è)置全局配置,定制分段的視覺(jué)映射 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(["中國(guó)", "英國(guó)", "美國(guó)"]) # 添加 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ì)性的數(shù)據(jù),為此 pyecharts 中提供了一種解決方案時(shí)間線。
如果說(shuō)一個(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(["中國(guó)", "英國(guó)", "美國(guó)"]) bar1.add_yaxis("GDP", [30, 20, 10], label_opts=LabelOpts(position='right')) bar1.reversal_axis() bar2 = Bar() bar2.add_xaxis(["中國(guó)", "英國(guó)", "美國(guó)"]) bar2.add_yaxis("GDP", [50, 20, 30], label_opts=LabelOpts(position='right')) bar2.reversal_axis() bar3 = Bar() bar3.add_xaxis(["中國(guó)", "英國(guó)", "美國(guó)"]) 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.通過(guò)時(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: [[國(guó)家1, GDP], [國(guó)家2, GDP]], 年份2: [國(guó)家, GDP], ...} data_dict = dict() for line in data_lines: year = int(line.split(',')[0]) # 年份 country = line.split(',')[1] # 國(guó)家 gdp = float(line.split(',')[2]) # gdp 數(shù)據(jù),通過(guò) 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 可能是無(wú)序的) 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前八的國(guó)家 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國(guó)家", 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) ) # 通過(guò)時(shí)間線繪圖 timeline.render("1960~2019全球GDP前8國(guó)家.html")
到此這篇關(guān)于Python pyecharts 數(shù)據(jù)可視化模塊的文章就介紹到這了,更多相關(guān)Python pyecharts 數(shù)據(jù)可視化內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Kwargs傳遞給Python 中的另一個(gè)函數(shù)實(shí)現(xiàn)方法
Python 列出了可以傳遞給程序中的函數(shù)的兩種類型的參數(shù), 非關(guān)鍵字參數(shù)**args和關(guān)鍵字參數(shù) **kwargs ,在本文中,我們將討論如何使用關(guān)鍵字參數(shù)及如何將關(guān)鍵字參數(shù)傳遞給另一個(gè)函數(shù),感興趣的朋友跟隨小編一起看看吧2023-08-08django之靜態(tài)文件 django 2.0 在網(wǎng)頁(yè)中顯示圖片的例子
今天小編就為大家分享一篇django之靜態(tài)文件 django 2.0 在網(wǎng)頁(yè)中顯示圖片的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-07-07python+selenium對(duì)table表和分頁(yè)處理
這篇文章主要介紹了python+selenium對(duì)table表和分頁(yè)處理,文章內(nèi)容只要包括bulabula2022、table表分頁(yè)處理、網(wǎng)頁(yè)table所有內(nèi)容循環(huán)處理等相關(guān)內(nèi)容,需要的小伙伴可以參考一下2022-01-01matplotlib如何設(shè)置坐標(biāo)軸刻度的個(gè)數(shù)及標(biāo)簽的方法總結(jié)
這里介紹兩種設(shè)置坐標(biāo)軸刻度的方法,一種是利用pyplot提交的api去進(jìn)行設(shè)置,另一種是通過(guò)調(diào)用面向?qū)ο蟮腶pi, 即通過(guò)matplotlib.axes.Axes去設(shè)置,需要的朋友可以參考下2021-06-06Django集成搜索引擎Elasticserach的方法示例
這篇文章主要介紹了Django集成搜索引擎Elasticserach的方法示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06python 實(shí)現(xiàn)的發(fā)送郵件模板【普通郵件、帶附件、帶圖片郵件】
這篇文章主要介紹了python 實(shí)現(xiàn)的發(fā)送郵件模板,包含Python發(fā)送普通郵件、帶附件及帶圖片郵件相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-07-07新手學(xué)習(xí)Python2和Python3中print不同的用法
在本篇文章里小編給大家分享的是關(guān)于Python2和Python3中print不同的用法,有興趣的朋友們可以學(xué)習(xí)下。2020-06-06Python?matplotlib實(shí)戰(zhàn)之氣泡圖繪制
氣泡圖是一種多變量的統(tǒng)計(jì)圖表,可以看作是散點(diǎn)圖的變形,這篇文章主要為大家介紹了如何使用Matplotlib繪制氣泡圖,需要的小伙伴可以參考下2023-08-08