日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

python可視化數據分析pyecharts初步嘗試_python

作者:坦先生的AI資料室 ? 更新時間: 2022-06-06 編程語言

有一個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

欄目分類
最近更新