妙??!這款 Python 數(shù)據(jù)可視化工具強(qiáng)的很!
使用 Altair ,你可以將更多時(shí)間專注于數(shù)據(jù)及其含義,下面我將詳細(xì)介紹:
示例
這是一個(gè)在 JupyterLab 中使用 Altair 快速可視化和顯示數(shù)據(jù)集的示例:
import altair as alt
# load a simple dataset as a pandas DataFrame
from vega_datasets import data
cars = data.cars()
alt.Chart(cars).mark_point().encode(
x='Horsepower',
y='Miles_per_Gallon',
color='Origin',
)
源自 Vega-Lite 的 Altair 的獨(dú)特功能之一是聲明性語(yǔ)法,它不僅具有可視化功能,還具有交互性。通過(guò)對(duì)上面的示例進(jìn)行一些修改,我們可以創(chuàng)建一個(gè)鏈接的直方圖,該直方圖根據(jù)散點(diǎn)圖的選擇進(jìn)行過(guò)濾。
import altair as alt
from vega_datasets import data
source = data.cars()
brush = alt.selection(type='interval')
points = alt.Chart(source).mark_point().encode(
x='Horsepower',
y='Miles_per_Gallon',
color=alt.condition(brush, 'Origin', alt.value('lightgray'))
).add_selection(
brush
)
bars = alt.Chart(source).mark_bar().encode(
y='Origin',
color='Origin',
x='count(Origin)'
).transform_filter(
brush
)
points & bars
安裝方法
Altair需要以下依賴項(xiàng):
- pandas
- traitlets
- IPython
如果已克隆存儲(chǔ)庫(kù),請(qǐng)從存儲(chǔ)庫(kù)的根目錄運(yùn)行以下命令:
pip install -e .[dev]
如果你不想克隆存儲(chǔ)庫(kù),則可以使用以下命令進(jìn)行安裝:
pip install git+https://github.com/altair-viz/altair
更多內(nèi)容詳情,可以查看github鏈接:
https://github.com/altair-viz/altair
三大操作
接下來(lái),我將詳細(xì)地介紹 Altair 如何創(chuàng)建過(guò)濾、分組和合并操作的可視化對(duì)象,可以將其用作探索性數(shù)據(jù)分析過(guò)程的一部分。
我們構(gòu)建兩個(gè)數(shù)據(jù)幀的模擬數(shù)據(jù)。第一個(gè)是餐廳訂單,第二個(gè)是餐廳訂單中的商品價(jià)格。
# import libraries
import numpy as np
import pandas as pd
import altair as alt
import random
# mock data
orders = pd.DataFrame({
"order_id": np.arange(1,101),
"item": np.random.randint(1, 50, size=100),
"qty": np.random.randint(1, 10, size=100),
"tip": (np.random.random(100) * 10).round(2)
})
prices = pd.DataFrame({
"item": np.arange(1,51),
"price": (np.random.random(50) * 50).round(2)
})
order_type = ["lunch", "dinner"] * 50
random.shuffle(order_type)
orders["order_type"] = order_type
首先,我們創(chuàng)建一個(gè)簡(jiǎn)單的圖來(lái) Altair 語(yǔ)法結(jié)構(gòu)。
alt.Chart(orders).mark_circle(size=50).encode(
x="qty", y="tip", color="order_type"
).properties(
title = "Tip vs Quantity"
)
Altair 基本語(yǔ)法四步曲:
- 將數(shù)據(jù)傳遞到 Chart 對(duì)象,數(shù)據(jù)可以采用Pandas數(shù)據(jù)框或指向json或csv文件的URL字符串的形式。
- 選擇可視化的類型(例如 mark_circle,mark_line 等)。
- encode 編碼函數(shù)指定在給定數(shù)據(jù)幀中要繪制的內(nèi)容。因此,我們?cè)诰幋a函數(shù)中編寫的任何內(nèi)容都必須鏈接到數(shù)據(jù)幀。
- 使用properties函數(shù)指定圖的某些屬性。
考慮這樣一種情況,我們需要?jiǎng)?chuàng)建 pirce 和 tip 值的散點(diǎn)圖,它們位于不同的數(shù)據(jù)幀中。一種選擇是合并兩個(gè)數(shù)據(jù)幀,并在散點(diǎn)圖中使用這兩列。
Altair提供了一種更實(shí)用的方法,它允許在其他數(shù)據(jù)框中查找列, 類似 Pandas 的 merge 函數(shù)功能相同。
alt.Chart(orders).mark_circle(size=50).encode(
x="tip", y="price:Q", color="order_type"
).transform_lookup(
lookup="item",
from_=alt.LookupData(data=prices, key="item", fields=["price"])
).properties(
title = "Price vs Tip"
)
transform_lookup 函數(shù)類似于 Pandas 的 merge 函數(shù)。用于匹配觀察值的列(即行)將傳遞給lookup參數(shù)。fields參數(shù)用于從另一個(gè)數(shù)據(jù)幀中選擇所需的列。
我們還可以把過(guò)濾組件集成到繪圖中,讓我們繪制價(jià)格超過(guò)10美元的數(shù)據(jù)點(diǎn)。
alt.Chart(orders).mark_circle(size=50).encode(
x="tip", y="price:Q", color="order_type"
).transform_lookup(
lookup="item",
from_=alt.LookupData(data=prices, key="item", fields=["price"])
).transform_filter(
alt.FieldGTPredicate(field='price', gt=10)
).properties(
title = "Price vs Tip"
)
transform_filter 函數(shù)用于過(guò)濾。FieldGTPredicate處理"大于"的條件。
除了過(guò)濾和合并外,Altair 還允許在繪圖之前對(duì)數(shù)據(jù)點(diǎn)進(jìn)行分組。例如,我們可以創(chuàng)建一個(gè)條形圖來(lái)顯示每種訂單類型的商品平均價(jià)格。此外,我們可以對(duì)價(jià)格低于20美元的商品執(zhí)行此操作。
alt.Chart(orders).mark_bar().encode(
y="order_type", x="avg_price:Q"
).transform_lookup(
lookup="item",
from_=alt.LookupData(data=prices, key="item", fields=["price"])
).transform_filter(
alt.FieldLTPredicate(field='price', lt=20)
).transform_aggregate(
avg_price = "mean(price)", groupby = ["order_type"]
).properties(
height=200, width=300
)
讓我們?cè)敿?xì)說(shuō)明每個(gè)步驟:
- transform_lookup:從價(jià)格數(shù)據(jù)框中查找價(jià)格。
- transform_filter:過(guò)濾價(jià)格低于20美元的價(jià)格。
- transform_aggregate:按訂單類型對(duì)價(jià)格進(jìn)行分組并計(jì)算均值。
結(jié)論
Altair 與其他常見(jiàn)的可視化庫(kù)的不同之處在于,它可以無(wú)縫地將數(shù)據(jù)分析組件集成到可視化中,是一款非常實(shí)用的數(shù)據(jù)探索工具。
篩選、合并和分組對(duì)于探索性數(shù)據(jù)分析過(guò)程至關(guān)重要。Altair 允許在創(chuàng)建數(shù)據(jù)可視化時(shí)執(zhí)行所有這些操作。從這個(gè)意義上講,Altair也可以視為數(shù)據(jù)分析工具。如果你感興趣,趕快嘗試一下吧。