Plotly_Express:新一代的高階視覺化神器,精美動態視覺化圖表僅需一行程式碼!
原文:視覺化工具 Plotly_Express 作者:Peter
歡迎關注 @Python與資料探勘 ,專注 Python、資料分析、資料探勘、好玩工具!
1、Plotly_Express快速入門
Plotly_Express
是新一代的高階視覺化神器,它是plotly。py的高階封裝,內建了大量實用、現代的繪圖模板。
使用者只需要呼叫簡單的API函式,便可快速地生成漂亮的動態視覺化圖表;同時其內建了很多的資料集,方便自行呼叫,快速模擬作圖。
本文講解主題如下圖,
歡迎收藏學習,喜歡點贊支援。
2、安裝
用如下命令可以安裝
plotly_express
pip install plotly_express
3、內建資料集
先匯入相關庫,進行檢視資料集:
import
pandas
as
pd
import
numpy
as
np
import
plotly_express
as
px
# 或 import plotly。express as px
GDP資料
記錄的是不同國家歷年GDP收入與人均壽命,包含的欄位:
國家country
洲continent
年份year
平均壽命lifeExp
人口數量pop
GDPgdpPercap
國家簡稱iso_alpha
國家編號iso_num
餐廳流水資料
餐廳的訂單流水資料,包含欄位:
總賬單費用bill
小費tip
顧客性別sex
顧客是否抽菸smoker
就餐日期day
就餐時間time
就餐人數size
鳶尾花資料集
著名的鳶尾花資料集,包含欄位:
萼片長sepal_length
萼片寬sepal_width
花瓣長petal_length
花瓣寬petal_width
花的種類species
種類所屬編號species_id
風力資料
一份關於風力等級的資料:
方向direction
強度strength
頻率frequency
選舉投票結果
該資料集記錄的是2013年
蒙特利爾市長選舉
投票結果,包含的主要欄位:
區域district
Coderre票數
Bergeron票數
Joly票數
總票數total
勝者winner
結果result
區編號district_id
汽車共享可用性資料
該資料記錄的是蒙特利爾一個區域中心附近的汽車共享服務的可用性,包含的欄位:
緯度centroid_lat
經度centroid_lon
汽車小時數car_hours
高峰小時peak_hour
股票資料
內建的一份股票資料,包含欄位:
日期date
6個公司名稱:GOOG、AAPL、AMZN、FB、NFLX、MSFT
4、內建顏色面板
plotly_express
還內建了很多顏色面板,顏色任你選擇,下面是各個主題下的部分截圖:
卡通片主題
px
。
colors
。
carto
。
swatches
()
CMOcean系列
px
。
colors
。
cmocean
。
swatches
()
ColorBrewer2系列
px
。
colors
。
colorbrewer
。
swatches
()
週期性色調
適用於具有自然週期結構的連續資料
px
。
colors
。
cyclical
。
swatches
()
分散色標
適用於具有自然中點的連續資料
px
。
colors
。
diverging
。
swatches
()
定性色標系列
適用於沒有自然順序的資料
px
。
colors
。
qualitative
。
swatches
()
image-20210325170234151
順序色標系列
漸變的顏色系列,適用於大多數連續資料
px
。
colors
。
sequential
。
swatches
()
5、作圖
下面介紹使用Plotly_express繪製常見的圖形,所有的圖形在jupyter notebook中都是動態視覺化的,本文中採用截圖展示。
柱狀圖
# 指定選取國家:Switzerland
Switzerland
=
gapminder
[
gapminder
[
“country”
]
==
“Switzerland”
]
Switzerland
# 資料顯示如下
px
。
bar
(
Switzerland
,
# 上面指定的資料
x
=
“year”
,
# 橫座標
y
=
“pop”
,
# 縱座標
color
=
“pop”
)
# 顏色取值
具體結果如下:
散點圖
先選取繪圖需要的資料:
# 寫法1
# gapminder_2002 = gapminder。query(“year==2002”)
# 寫法2
gapminder_2002
=
gapminder
[
gapminder
[
“year”
]
==
2002
]
gapminder_2002
px
。
scatter
(
gapminder_2002
,
# 傳入的資料集
x
=
“gdpPercap”
,
# 橫座標是人均GDP
y
=
“lifeExp”
,
# 縱座標是平均壽命
color
=
“continent”
# 顏色取值:根據洲的值來取
)
冒泡散點圖
px
。
scatter
(
gapminder_2002
# 繪圖DataFrame資料集
,
x
=
“gdpPercap”
# 橫座標
,
y
=
“lifeExp”
# 縱座標
,
color
=
“continent”
# 區分顏色
,
size
=
“pop”
# 區分圓的大小
,
size_max
=
60
# 散點大小
)
散點矩陣圖
px
。
scatter_matrix
(
iris
,
# 傳入繪圖資料
dimensions
=
[
“sepal_width”
,
“sepal_length”
,
“petal_width”
,
“petal_length”
],
# 維度設定
color
=
“species”
)
# 顏色取值
面積圖
# area 圖
px
。
area
(
gapminder
,
# 繪圖的資料集
x
=
“year”
,
# 橫軸資料
y
=
“pop”
,
# 縱軸資料
color
=
“continent”
,
# 顏色取值
line_group
=
“country”
)
# 線型分組
股票趨勢圖
# FB公司股票趨勢圖
px
。
line
(
stock
,
x
=
‘date’
,
y
=
“FB”
)
餅圖
1、我們使用小費tips資料,檢視前5行資料:
2、根據day分組,統計total_bill欄位的和
3、繪製餅圖,自動顯示每個day的佔比
px
。
pie
(
total_bill_byday
,
# 繪圖資料
names
=
“day”
,
# 每個組的名字
values
=
“total_bill”
# 組的取值
)
旭日圖
# 選取2002年資料
gapminder_2002
=
gapminder
[
gapminder
[
“year”
]
==
2002
]
px
。
sunburst
(
gapminder_2002
,
# 繪圖資料
path
=
[
‘continent’
,
‘country’
],
# 指定路徑:從洲到國家
values
=
‘pop’
,
# 資料大小:人口數
color
=
‘lifeExp’
,
# 顏色
hover_data
=
[
‘iso_alpha’
]
# 顯示資料
)
漏斗圖
漏斗圖形在網際網路的電商、使用者分群等領域使用的比較廣泛,自行模擬一個電商UV-付款轉化的資料繪圖:
data
=
dict
(
# 建立原始資料
number
=
[
1000
,
800
,
400
,
200
,
100
,
30
],
stage
=
[
“UV”
,
“搜尋”
,
“蒐藏”
,
“加購”
,
“下單”
,
“付款”
]
)
# 傳入資料和數軸
px
。
funnel
(
data
,
x
=
“number”
,
y
=
“stage”
)
加入一個顏色引數color,改變每個階段的顏色:
data
=
dict
(
# 建立原始資料
number
=
[
1000
,
800
,
400
,
200
,
100
,
30
],
stage
=
[
“UV”
,
“搜尋”
,
“蒐藏”
,
“加購”
,
“下單”
,
“付款”
]
)
# 傳入資料和數軸
px
。
funnel
(
data
,
x
=
“number”
,
y
=
“stage”
,
color
=
“number”
# 顏色設定
)
直方圖
px
。
histogram
(
tips
,
# 繪圖資料
x
=
“sex”
,
# 指定兩個數軸
y
=
“tip”
,
histfunc
=
“avg”
,
# 直方圖函式:均值
color
=
“smoker”
,
# 顏色取值
barmode
=
“group”
,
# 柱狀圖模式
facet_row
=
“time”
,
# 橫縱縱軸的欄位設定
facet_col
=
“day”
,
category_orders
=
{
“day”
:[
“Thur”
,
“Fri”
,
“Sat”
,
“Sun”
],
# 分類
“time”
:[
“Lunch”
,
“Dinner”
]})
聯合分佈圖
多種圖形的組合顯示:
px
。
scatter
(
iris
,
x
=
“sepal_width”
,
y
=
“sepal_length”
,
color
=
“species”
,
marginal_x
=
“histogram”
,
marginal_y
=
“rug”
)
箱型圖
# notched=True顯示連線處的錐形部分
px
。
box
(
tips
,
# 資料集
x
=
“day”
,
# 橫軸資料
y
=
“total_bill”
,
# 縱軸資料
color
=
“smoker”
,
# 顏色
notched
=
True
)
# 連線處的錐形部分顯示出來
小提琴圖
px
。
scatter
(
iris
,
# 傳入資料
x
=
“sepal_width”
,
# 設定XY軸
y
=
“sepal_length”
,
color
=
“species”
,
# 顏色取值
marginal_y
=
“violin”
,
# xy兩表圖形的設定:小提琴圖和箱型圖
marginal_x
=
“box”
,
trendline
=
“ols”
)
# 趨勢線設定
等高線圖
px
。
density_contour
(
iris
,
# 資料集
x
=
“sepal_width”
,
# xy軸
y
=
“sepal_length”
,
color
=
“species”
# 顏色取值
)
還可以繪製密度等值線圖;
px
。
density_heatmap
(
iris
,
# 傳入資料
x
=
“sepal_width”
,
# 兩個軸的資料設定
y
=
“sepal_length”
,
marginal_y
=
“rug”
,
# 邊緣圖形設定
marginal_x
=
“histogram”
# 在密度圖的基礎上,指定另外兩種圖形
)
密度熱力圖
資料的設定和密度等值圖相同,只是選擇的圖形種類不同:
px
。
density_heatmap
(
# 密度熱力圖
iris
,
x
=
“sepal_width”
,
y
=
“sepal_length”
,
marginal_y
=
“rug”
,
marginal_x
=
“histogram”
)
並行分類圖
px
。
parallel_categories
(
tips
,
# 傳入資料
color
=
“size”
,
# 顏色取值
color_continuous_scale
=
px
。
colors
。
sequential
。
Inferno
# 顏色變化趨勢
)
3D散點圖
使用的是選舉結果資料集:
px
。
scatter_3d
(
election
,
# 傳入資料集
x
=
“Joly”
,
# 指定XYZ座標軸的資料
y
=
“Coderre”
,
z
=
“Bergeron”
,
color
=
“winner”
,
# 顏色取值
size
=
“total”
,
# 大小取值
hover_name
=
“district_id”
,
# 指定顏色種類、大小和顯示名稱
symbol
=
“result”
,
# 右邊的圓形和菱形
color_discrete_map
=
{
“Joly”
:
“blue”
,
“Bergeron”
:
“green”
,
“Coderre”
:
“red”
}
# 改變預設顏色
)
3D線型圖
px
。
line_3d
(
election
,
# 繪圖資料集
x
=
“Joly”
,
# 3個座標軸
y
=
“Coderre”
,
z
=
“Bergeron”
,
color
=
“winner”
,
# 顏色和線型設定
line_dash
=
“winner”
)
基於地圖的圖形
基於choropleth分佈的地圖:
px
。
choropleth
(
gapminder
,
# 資料
locations
=
“iso_alpha”
,
# 簡稱
color
=
“lifeExp”
,
# 顏色取值
hover_name
=
“country”
,
# 懸停資料
animation_frame
=
“year”
,
# 播放按鈕設定
color_continuous_scale
=
px
。
colors
。
sequential
。
Plasma
,
# 顏色變化取值
projection
=
“natural earth”
# 使用的地圖設定
)
基於line_geo線型地圖:
px。line_geo(
gapminder_2002,
locations=“iso_alpha”,
color=“continent”,
projection=“orthographic”)
矩陣式樹狀結構圖
矩陣式樹狀結構圖是一種用於分層資料的複雜、基於區域的資料展示圖形:
# 選取2002年資料
gapminder_2002
=
gapminder
[
gapminder
[
“year”
]
==
2002
]
px
。
treemap
(
gapminder_2002
,
# 資料
path
=
[
px
。
Constant
(
‘world’
),
‘continent’
,
‘country’
],
# 繪圖路徑:world——-continent——-country
values
=
‘pop’
,
# 資料取值
color
=
‘pop’
,
# 顏色取值
hover_data
=
[
‘iso_alpha’
])
# 顯示資料:國家簡稱
散點極座標圖
px
。
scatter_polar
(
# 散點極座標
wind
,
# 資料集
r
=
“frequency”
,
# 半徑
theta
=
“direction”
,
# 角度
color
=
“strength”
,
# 顏色
symbol
=
“strength”
,
# 符號
color_discrete_sequence
=
px
。
colors
。
sequential
。
Plasma_r
)
# 顏色
線性極座標圖
px
。
line_polar
(
# 線性極座標
wind
,
# 資料集
r
=
“frequency”
,
# 半徑
theta
=
“direction”
,
# 角度
color
=
“strength”
,
# 顏色
line_close
=
True
,
# 線性閉合
color_discrete_sequence
=
px
。
colors
。
sequential
。
Plasma_r
)
# 顏色
柱狀極座標圖
px
。
bar_polar
(
# 柱狀圖極座標圖
wind
,
# 資料集
r
=
“frequency”
,
# 半徑
theta
=
“direction”
,
# 角度
color
=
“strength”
,
# 顏色
template
=
“plotly_dark”
,
# 主題
color_discrete_sequence
=
px
。
colors
。
sequential
。
Plasma_r
)
# 顏色
6、內建主題
Plotly_Express內建了3種主題可供選擇:
plotly
plotly_white
plotly_dark
px
。
scatter
(
gapminder_2002
,
# 傳入的資料集
x
=
“gdpPercap”
,
# 橫座標是人均GDP
y
=
“lifeExp”
,
# 縱座標是平均壽命
color
=
“continent”
,
# 顏色取值:根據洲的值來取
template
=
“plotly”
# 分別主題設定為:plotly、plotly_dark
)
總結
本文詳細介紹了一個新的高階視覺化庫Plotly_Express,從其簡介、安裝、內建的顏色面板、主題到各種圖形的繪製。
這個庫最大的特點:
程式碼量非常少,圖形種類全,基本上一行程式碼就能繪製出非常精美的動態視覺化圖形
。以後會介紹更多關於plotly_express的使用文章,特別是
plotly
和
dash
的結合,更是無比強大。敬請期待!
文章推薦
40個 Python 案例玩轉資料視覺化,強烈建議收藏!
中槍了?六種讓你的 Python 程式變慢的壞習慣
PySnooper:永遠不要使用 print 進行除錯
數分實戰 |《掃黑風暴》值不值得看,資料告訴你答案!
教你3個python「效能分析」工具,再也不用自己計算函式耗時了
超越 Facebook 的 Prophet,NeuralProphet 這個時序工具包也太強了!
乾貨!20張最新視覺化大屏模板,各行業資料直接套用(含原始碼)
用 Python 寫出這樣的進度條,重新整理了我對進度條的認知!
Schedule:一個簡單實用的 Python 週期任務排程工具!
Rich:Python開發者的完美終端工具!
再次出發!FaceBook 開源“一站式服務”時序利器 Kats !
超級乾貨!史上最全資料分析學習路線(附資源下載)
整理不易,有所收穫,點個贊和愛心
❤️,
更多精彩歡迎關注