作者:俊欣
來源:關於數據分析與可視化
說到 Python 當中的可視化模塊,相信大家用的比較多的還是 matplotlib 、 seaborn 等模塊,今天小編來嘗試用 Plotly 模塊為大家繪製可視化圖表,和前兩者相比,用 Plotly 模塊會指出來的可視化圖表有著很強的交互性。
柱狀圖
我們先導入後面需要用到的模塊並且生成一批假數據,
import numpy as np
import plotly.graph_objects as go
# create dummy data
vals = np.ceil(100 * np.random.rand(5)).astype(int)
keys = [ "A", "B", "C", "D", "E"]
我們基於所生成的假數據來繪製柱狀圖,代碼如下
fig = go.Figure
fig.add_trace(
go.Bar(x=keys, y=vals)
)
fig.update_layout(height=600, width=600)
fig.show
output
可能讀者會感覺到繪製出來的圖表略顯簡單,我們再來完善一下,添加上標題和註解,代碼如下
# create figure
fig = go.Figure
# 繪製圖表
fig.add_trace(
go.Bar(x=keys, y=vals, hovertemplate= "Key: %{x}
Value: %{y}
)
# 更新完善圖表
fig.update_layout(
font_family= "Averta",
hoverlabel_font_family= "Averta",
title_text= "直方圖",
xaxis_title_text= "X軸-鍵",
xaxis_title_font_size=18,
xaxis_tickfont_size=16,
yaxis_title_text= "Y軸-值",
yaxis_title_font_size=18,
yaxis_tickfont_size=16,
hoverlabel_font_size=16,
height=600,
width=600
)
fig.show
output
分組條形圖和堆積條形圖
例如我們有多組數據想要繪製成柱狀圖的話,我們先來創建好數據集
vals_2 = np.ceil(100 * np.random.rand(5)).astype(int)
vals_3 = np.ceil(100 * np.random.rand(5)).astype(int)
vals_array = [vals, vals_2, vals_3]
然後我們遍歷獲取列表中的數值並且繪製成條形圖,代碼如下
# 生成畫布
fig = go.Figure
# 繪製圖表
fori, vals inenumerate(vals_array):
fig.add_trace(
go.Bar(x=keys, y=vals, name=f "Group {i+1}", hovertemplate=f "Group {i+1}
Key: %{{x}}
Value: %{{y}}
)
# 完善圖表
fig.update_layout(
barmode= "group",
......
)
fig.show
output
而我們想要變成堆積狀的條形圖,只需要修改代碼中的一處即可,將 fig.update_layout(barmode="group") 修改成 fig.update_layout(barmode="group") 即可,我們來看一下出來的樣子
箱型圖
箱型圖在數據統計分析當中也是應用相當廣泛的,我們先來創建兩個假數據
# create dummy data for boxplots
y1 = np.random.normal(size=1000)
y2 = np.random.normal(size=1000)
我們將上面生成的數據繪製成箱型圖,代碼如下
# 生成畫布
fig = go.Figure
# 繪製圖表
fig.add_trace(
go.Box(y=y1, name= "Dataset 1"),
)
fig.add_trace(
go.Box(y=y2, name= "Dataset 2"),
)
fig.update_layout(
......
)
fig.show
output
散點圖和氣泡圖
接下來我們嘗試來繪製一張散點圖,也是一樣的步驟,我們想嘗試生成一些假數據,代碼如下
x = [i fori inrange(1, 10)]
y = np.ceil(1000 * np.random.rand(10)).astype(int)
然後我們來繪製散點圖,調用的是 Scatter 方法,代碼如下
# create figure
fig = go.Figure
fig.add_trace(
go.Scatter(x=x, y=y, mode= "markers", hovertemplate= "x: %{x}
y: %{y}
)
fig.update_layout(
.......
)
fig.show
output
那麼氣泡圖的話就是在散點圖的基礎上,根據數值的大小來設定散點的大小,我們再來創建一些假數據用來設定散點的大小,代碼如下
s = np.ceil(30 * np.random.rand(5)).astype(int)
我們將上面用作繪製散點圖的代碼稍作修改,通過 marker_size 參數來設定散點的大小,如下所示
fig = go.Figure
fig.add_trace(
go.Scatter(x=x, y=y, mode= "markers", marker_size=s, text=s, hovertemplate= "x: %{x}
y: %{y}
Size: %{text}
)
fig.update_layout(
......
)
fig.show
output
直方圖
直方圖相比較於上面提到的幾種圖表,總體上來說會稍微有點丑,但是通過直方圖,讀者可以更加直觀地感受到數據的分布,我們先來創建一組假數據,代碼如下
## 創建假數據
data = np.random.normal(size=1000)
然後我們來繪製直方圖,調用的是 Histogram 方法,代碼如下
# 創建畫布
fig = go.Figure
# 繪製圖表
fig.add_trace(
go.Histogram(x=data, hovertemplate= "Bin Edges: %{x}
Count: %{y}
)
fig.update_layout(
height=600,
width=600
)
fig.show
output
我們再在上述圖表的基礎之上再進行進一步的格式優化,代碼如下
# 生成畫布
fig = go.Figure
# 繪製圖表
fig.add_trace(
go.Histogram(x=data, histnorm= "probability", hovertemplate= "Bin Edges: %{x}
Count: %{y}
)
fig.update_layout(
......
)
fig.show
output
多個子圖拼湊到一塊兒
相信大家都知道在 matplotlib 模塊當中的 subplots 方法可以將多個子圖拼湊到一塊兒,那麼同樣地在 plotly 當中也可以同樣地將多個子圖拼湊到一塊兒,調用的是 plotly 模塊當中 make_subplots 函數
from plotly.subplots import make_subplots
## 2行2列的圖表
fig = make_subplots(rows=2, cols=2)
## 生成一批假數據用於圖表的繪製
x = [i fori inrange(1, 11)]
y = np.ceil(100 * np.random.rand(10)).astype(int)
s = np.ceil(30 * np.random.rand(10)).astype(int)
y1 = np.random.normal(size=5000)
y2 = np.random.normal(size=5000)
接下來我們將所要繪製的圖表添加到 add_trace 方法當中,代碼如下
# 繪製圖表
fig.add_trace(
go.Bar(x=x, y=y, hovertemplate= "x: %{x}
y: %{y}
row=1, col=1
)
fig.add_trace(
go.Histogram(x=y1, hovertemplate= "Bin Edges: %{x}
Count: %{y}
row=1, col=2
)
fig.add_trace(
go.Scatter(x=x, y=y, mode= "markers", marker_size=s, text=s, hovertemplate= "x: %{x}
y: %{y}
Size: %{text}
row=2, col=1
)
fig.add_trace(
go.Box(y=y1, name= "Dataset 1"),
row=2, col=2
)
fig.add_trace(
go.Box(y=y2, name= "Dataset 2"),
row=2, col=2
)
fig.update_xaxes(title_font_size=18, tickfont_size=16)
fig.update_yaxes(title_font_size=18, tickfont_size=16)
fig.update_layout(
......
)
fig.show
output
點這裡關注我,記得標星哦~
CDA課程諮詢