Plotly_Express是新一代的高級可視化神器,它是plotly.py的高級封裝,內置了大量實用、現代的繪圖模板。
使用者只需要調用簡單的API函數,便可快速地生成漂亮的動態可視化圖表;同時其內置了很多的數據集,方便自行調用,快速模擬作圖。
安裝用pip install plotly_express 命令可以安裝plotly_express
pip install plotly_express
內置數據集先導入相關庫,進行查看數據集:
import pandas as pd
import numpy as np
import plotly_express as px # 或 import plotly.express as px
GDP數據記錄的是不同國家歷年GDP收入與人均壽命,包含的欄位:
餐廳流水數據
餐廳的訂單流水數據,包含欄位:
鳶尾花數據集
著名的鳶尾花數據集,包含欄位:
風力數據
一份關於風力等級的數據:
選舉投票結果
該數據集記錄的是2013年蒙特婁市長選舉投票結果,包含的主要欄位:
汽車共享可用性數據
該數據記錄的是蒙特婁一個區域中心附近的汽車共享服務的可用性,包含的欄位:
股票數據
內置的一份股票數據,包含欄位:
6個公司名稱:GOOG、AAPL、AMZN、FB、NFLX、MSFT內置顏色面板
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()image-20210325170457557作圖
下面介紹使用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_2002px.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) # 顏色內置主題
Plotly_Express內置了3種主題可供選擇:
px.scatter(gapminder_2002, # 傳入的數據集
x="gdpPercap", # 橫坐標是人均GDP
y="lifeExp", # 縱坐標是平均壽命
color="continent", # 顏色取值:根據洲的值來取
template="plotly" # 分別主題設置為:plotly、plotly_dark
)總結
本文詳細介紹了一個新的高級可視化庫Plotly_Express,從其簡介、安裝、內置的顏色面板、主題到各種圖形的繪製。
這個庫最大的特點:代碼量非常少,圖形種類全,基本上一行代碼就能繪製出非常精美的動態可視化圖形。以後會介紹更多關於plotly_express的使用文章,特別是plotly和dash的結合,更是無比強大。敬請期待!