網站首頁 編程語言 正文
有一個web+flask項目需要可視化數據分析結果,檢索后發現,pyecharts工具包非常對口。
Echarts 是一個由百度開源的數據可視化,憑借著良好的交互性,精巧的圖表設計,得到了眾多開發者的認可。而 Python 是一門富有表達力的語言,很適合用于數據處理。當數據分析遇上數據可視化時,pyecharts 誕生了。
pyecharts中文文檔有詳細的說明,這里記錄了個人更感興趣的部分和對應的使用結果。
整體說明
pyecharts繪圖的步驟可以簡化成:
新建合適的圖表對象,常見的有:
Pie: 餅圖
Bar: 柱狀圖/條狀圖
Boxplot: 箱形圖
HeatMap: 熱力圖
Line: 折線圖/面積圖
Scatter: 散點圖
特別的,可以把多個圖合在一起的 Overlap: 層疊多圖
更詳細的可以參考官方文檔-圖表類型
bar = Bar()
后續的操作都是利用這個對象的方法進行。
為圖表對象增加數據,比如 增加x軸(.add_xaxis)、y軸數(.add_yaxis)
bar.add_xaxis(["襯衫", "羊毛衫", "雪紡衫", "褲子", "高跟鞋", "襪子"]) bar.add_yaxis("商家A", [5, 20, 36, 10, 75, 90])
全局配置項:所有的內容都是通過.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
箱型圖,一種比較簡潔的統計值可視化方法
import random from pyecharts import options as opts from pyecharts.charts import Boxplot import numpy as np # 離線資源,有網絡下可以不管 from pyecharts.globals import CurrentConfig CurrentConfig.ONLINE_HOST = "http://127.0.0.1:8889/assets/" # 長度為1的str list x_label = ['隨機數'] data = np.random.randint(1000, size=100) # 這里data應為2維數組,長度和x_label相同的 list list data = [data.tolist()] boxplot = Boxplot() boxplot.add_xaxis(x) # 調用自帶的函數,計算箱型圖需要的數據 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比較簡單,適合入門,設定一個x軸,一個y軸,就可以render了
# -*- coding: utf-8 -*- from pyecharts.charts import Bar from pyecharts import options as opts import numpy as np # 離線資源,有網絡下可以不管 from pyecharts.globals import CurrentConfig CurrentConfig.ONLINE_HOST = "http://127.0.0.1:8889/assets/" # 隨機數組,0~255的數字,10000個 x = np.random.randint(255, size=1000) # 統計直方圖 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軸 頻數, 這里的list一定要是標準int,不能為 np.int,所有 y_axis=list(sum)的話是不可以的 bar.add_yaxis(series_name='頻數', y_axis=sum.tolist()) # 設置標題 bar.set_global_opts(title_opts=opts.TitleOpts(title='直方圖統計')) # 生成網頁,會在當前目錄下生成一個render.html bar.render()
HeatMap
熱力圖
這篇已經敘述的很好了,以下為引用
注,引文中的代碼是用鏈式寫的,官方是這么推薦的。
import random from pyecharts import options as opts from pyecharts.charts import HeatMap from pyecharts.faker import Faker # 離線資源,有網絡下可以不管 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="基礎熱力圖"), visualmap_opts=opts.VisualMapOpts(), ) ) heatmap.render()
原文鏈接:https://www.cnblogs.com/yushengchn/p/15909367.html
相關推薦
- 2023-07-28 Cannot read properties of undefined (reading ‘push
- 2022-01-05 npm install 報錯:npm ERR! code EPERM npm ERR! syscal
- 2022-12-29 Redis并發訪問問題詳細講解_Redis
- 2021-12-13 C語言輸出唯一的子串_C 語言
- 2022-05-09 C#多線程之線程池ThreadPool用法_C#教程
- 2022-06-10 Flutter實現心動的動畫特效_Android
- 2022-03-22 C語言寫一個散列表_C 語言
- 2021-12-02 Oracle數據庫產重啟服務和監聽程序命令介紹_oracle
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支