python可視化數(shù)據(jù)分析pyecharts初步嘗試
有一個web+flask項目需要可視化數(shù)據(jù)分析結(jié)果,檢索后發(fā)現(xiàn),pyecharts工具包非常對口。
Echarts 是一個由百度開源的數(shù)據(jù)可視化,憑借著良好的交互性,精巧的圖表設(shè)計,得到了眾多開發(fā)者的認可。而 Python 是一門富有表達力的語言,很適合用于數(shù)據(jù)處理。當數(shù)據(jù)分析遇上數(shù)據(jù)可視化時,pyecharts 誕生了。
pyecharts中文文檔有詳細的說明,這里記錄了個人更感興趣的部分和對應的使用結(jié)果。
整體說明
pyecharts繪圖的步驟可以簡化成:
新建合適的圖表對象,常見的有:
Pie: 餅圖
Bar: 柱狀圖/條狀圖
Boxplot: 箱形圖
HeatMap: 熱力圖
Line: 折線圖/面積圖
Scatter: 散點圖
特別的,可以把多個圖合在一起的 Overlap: 層疊多圖
更詳細的可以參考官方文檔-圖表類型
bar = Bar()
后續(xù)的操作都是利用這個對象的方法進行。
為圖表對象增加數(shù)據(jù),比如 增加x軸(.add_xaxis)、y軸數(shù)(.add_yaxis)
bar.add_xaxis(["襯衫", "羊毛衫", "雪紡衫", "褲子", "高跟鞋", "襪子"]) bar.add_yaxis("商家A", [5, 20, 36, 10, 75, 90])
全局配置項:所有的內(nèi)容都是通過.set_global_opts方法添加.set_global_opts()
bar.set_global_opts(title_opts=opts.TitleOpts(title="主標題", subtitle="副標題"))
常用的有
TitleOpts:標題配置項
LegendOpts:圖例配置項
VisualMapOpts:視覺映射配置項
AxisLineOpts: 坐標軸軸線配置項
AxisTickOpts: 坐標軸刻度配置項
AxisPointerOpts: 坐標軸指示器配置項
AxisOpts:坐標軸配置項
SingleAxisOpts:單軸配置項
詳見官方文檔配置項-全局配置項
例子
Boxplot
箱型圖,一種比較簡潔的統(tǒng)計值可視化方法
import random from pyecharts import options as opts from pyecharts.charts import Boxplot import numpy as np # 離線資源,有網(wǎng)絡(luò)下可以不管 from pyecharts.globals import CurrentConfig CurrentConfig.ONLINE_HOST = "http://127.0.0.1:8889/assets/" # 長度為1的str list x_label = ['隨機數(shù)'] data = np.random.randint(1000, size=100) # 這里data應為2維數(shù)組,長度和x_label相同的 list list data = [data.tolist()] boxplot = Boxplot() boxplot.add_xaxis(x) # 調(diào)用自帶的函數(shù),計算箱型圖需要的數(shù)據(jù) y_value = boxplot.prepare_data(y_value) boxplot.add_yaxis('', y_value) boxplot.set_global_opts(title_opts=opts.TitleOpts(title='box plot demo')) boxplot.render()
Bar
Bar比較簡單,適合入門,設(shè)定一個x軸,一個y軸,就可以render了
# -*- coding: utf-8 -*- from pyecharts.charts import Bar from pyecharts import options as opts import numpy as np # 離線資源,有網(wǎng)絡(luò)下可以不管 from pyecharts.globals import CurrentConfig CurrentConfig.ONLINE_HOST = "http://127.0.0.1:8889/assets/" # 隨機數(shù)組,0~255的數(shù)字,10000個 x = np.random.randint(255, size=1000) # 統(tǒng)計直方圖 sum = np.zeros(256, dtype=np.int32) for cur_x in x: sum[cur_x] += 1 # 繪圖 bar = Bar() # x軸 0~255 x_label = [str(label) for label in list(range(256))] bar.add_xaxis(x_label) # y軸 頻數(shù), 這里的list一定要是標準int,不能為 np.int,所有 y_axis=list(sum)的話是不可以的 bar.add_yaxis(series_name='頻數(shù)', y_axis=sum.tolist()) # 設(shè)置標題 bar.set_global_opts(title_opts=opts.TitleOpts(title='直方圖統(tǒng)計')) # 生成網(wǎng)頁,會在當前目錄下生成一個render.html bar.render()
HeatMap
熱力圖
這篇已經(jīng)敘述的很好了,以下為引用
注,引文中的代碼是用鏈式寫的,官方是這么推薦的。
import random from pyecharts import options as opts from pyecharts.charts import HeatMap from pyecharts.faker import Faker # 離線資源,有網(wǎng)絡(luò)下可以不管 from pyecharts.globals import CurrentConfig CurrentConfig.ONLINE_HOST = "http://127.0.0.1:8889/assets/" value = [[i, j, random.randint(0, 50)] for i in range(24) for j in range(7)] heatmap = ( HeatMap() .add_xaxis(Faker.clock) .add_yaxis( "", Faker.week, value, label_opts=opts.LabelOpts(is_show=True, position="inside"), ) .set_global_opts( title_opts=opts.TitleOpts(title="基礎(chǔ)熱力圖"), visualmap_opts=opts.VisualMapOpts(), ) ) heatmap.render()
以上就是python可視化數(shù)據(jù)分析pyecharts初步嘗試的詳細內(nèi)容,更多關(guān)于python可視化數(shù)據(jù)分析pyecharts的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python?Pandas中DataFrame.drop_duplicates()刪除重復值詳解
在實際處理數(shù)據(jù)中,數(shù)據(jù)預處理操作中,常常需要去除掉重復的數(shù)據(jù),這篇文章主要給大家介紹了關(guān)于Python?Pandas中DataFrame.drop_duplicates()刪除重復值的相關(guān)資料,需要的朋友可以參考下2022-07-07Python中bytes字節(jié)串和string字符串之間的轉(zhuǎn)換方法
python中字節(jié)字符串不能格式化,獲取到的網(wǎng)頁有時候是字節(jié)字符串,需要轉(zhuǎn)化后再解析,下面這篇文章主要給大家介紹了關(guān)于Python中bytes字節(jié)串和string字符串之間的轉(zhuǎn)換方法,需要的朋友可以參考下2022-01-01