Altair 是一個基于Jupyter Notebook的強大可視化庫。它提供了強大而簡潔的可視化語法,使我們能夠快速構(gòu)建各種統(tǒng)計可視化圖表。
通過下面10行代碼,你就能創(chuàng)建一個可交互的散點圖:
import altair as alt
from vega_datasets import data
cars = data.cars()
alt.Chart(cars).mark_point().encode(
x='Horsepower',
y='Miles_per_Gallon',
color='Origin',
).interactive()
1.準備
開始之前,你要確保Python和pip已經(jīng)成功安裝在電腦上,如果沒有,可以訪問這篇文章:超詳細Python安裝指南 進行安裝。
**(可選1) **如果你用Python的目的是數(shù)據(jù)分析,可以直接安裝Anaconda:Python數(shù)據(jù)分析與挖掘好幫手—Anaconda,它內(nèi)置了Python和pip.
**(可選2) **此外,推薦大家用VSCode編輯器,它有許多的優(yōu)點:Python 編程的最好搭檔—VSCode 詳細指南。
請選擇以下任一種方式輸入命令安裝依賴 :
- Windows 環(huán)境 打開 Cmd (開始-運行-CMD)。
- MacOS 環(huán)境 打開 Terminal (command+空格輸入Terminal)。
- 如果你用的是 VSCode編輯器 或 Pycharm,可以直接使用界面下方的Terminal.
pip install altair vega_datasets
2.基本使用
Altair 中的數(shù)據(jù)是圍繞 Pandas Dataframe 構(gòu)建的。
我們首先導入 Pandas 并創(chuàng)建一個簡單的 DataFrame 以進行可視化,a 列中有一個分類變量,b 列有一個數(shù)值變量:
import pandas as pd
data = pd.DataFrame({'a': list('CCCDDDEEE'),
'b': [2, 7, 4, 1, 2, 6, 8, 4, 7]})
Altair 中的基本對象是Chart,它將上述的數(shù)據(jù)作為單個參數(shù):
import altair as alt
chart = alt.Chart(data)
到目前為止,我們已經(jīng)定義了 Chart 對象,但是我們還沒有告訴圖表對數(shù)據(jù)做任何事情。接下來會出現(xiàn)。
有了這個圖表對象,我們現(xiàn)在可以指定我們希望如何可視化數(shù)據(jù),比如作為點:
alt.Chart(data).mark_point()
然后對數(shù)據(jù)進行編碼,比如指定 a 列為x,b列為y:
alt.Chart(data).mark_point().encode(
x='a', y='b'
)
效果如下:
如果你希望聚合求得某列得平均值,你還可以這么做:
alt.Chart(data).mark_point().encode(
x='a',
y='average(b)'
)
如果你希望使用柱狀圖,只需要把mark_point改為mark_bar:
alt.Chart(data).mark_bar().encode(
x='a',
y='average(b)'
)
還可以獲得水平柱狀圖,我們只需要把x和y對調(diào)一下:
alt.Chart(data).mark_bar().encode(
y='a',
x='average(b)'
)
除了點狀圖和柱狀圖,Altair 還支持幾十種圖表類型:
更多的圖表類型請在官網(wǎng)查看:
https://altair-viz.github.io/gallery/index.html
3.高級使用
你可以給圖表自定義你喜歡的顏色和對應的橫坐標縱坐標標題:
alt.Chart(data).mark_bar(color='firebrick').encode(
alt.Y('a', title='category'),
alt.X('average(b)', title='avg(b) by category')
)
你還可以將圖表保存為HTML:
chart = alt.Chart(data).mark_bar().encode(
x='a',
y='average(b)',
)
chart.save('chart.html')
如果你希望能夠通過區(qū)間選擇數(shù)據(jù)點并計數(shù),你可以這么做:
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還可以做多圖表聯(lián)動:
# 公眾號:Python實用寶典 整合
import altair as alt
from vega_datasets import data
cars = data.cars.url
brush = alt.selection_interval()
chart = alt.Chart(cars).mark_point().encode(
y='Horsepower:Q',
color=alt.condition(brush, 'Origin:N', alt.value('lightgray'))
).properties(
width=250,
height=250
).add_selection(
brush
)
chart.encode(x='Acceleration:Q') | chart.encode(x='Miles_per_Gallon:Q')
左邊圈起來的 Acceleration 數(shù)據(jù)點,右邊會對應顯示其 Miles_per_Gallon 數(shù)據(jù)點:
除了這些,Altair還有更多的交互功能,比如選擇框拖動、比例綁定、自動響應、表達式選擇等等。
-
數(shù)據(jù)分析
+關(guān)注
關(guān)注
2文章
1460瀏覽量
34144 -
python
+關(guān)注
關(guān)注
56文章
4807瀏覽量
85016 -
Altair
+關(guān)注
關(guān)注
0文章
18瀏覽量
10020 -
數(shù)據(jù)可視化
+關(guān)注
關(guān)注
0文章
471瀏覽量
10360
發(fā)布評論請先 登錄
相關(guān)推薦
評論