Altair 是一個(gè)基于Jupyter Notebook的強(qiáng)大可視化庫(kù)。它提供了強(qiáng)大而簡(jiǎn)潔的可視化語(yǔ)法,使我們能夠快速構(gòu)建各種統(tǒng)計(jì)可視化圖表。
通過(guò)下面10行代碼,你就能創(chuàng)建一個(gè)可交互的散點(diǎn)圖:
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.準(zhǔn)備
開(kāi)始之前,你要確保Python和pip已經(jīng)成功安裝在電腦上,如果沒(méi)有,可以訪問(wèn)這篇文章:超詳細(xì)Python安裝指南 進(jìn)行安裝。
**(可選1) **如果你用Python的目的是數(shù)據(jù)分析,可以直接安裝Anaconda:Python數(shù)據(jù)分析與挖掘好幫手—Anaconda,它內(nèi)置了Python和pip.
**(可選2) **此外,推薦大家用VSCode編輯器,它有許多的優(yōu)點(diǎn):Python 編程的最好搭檔—VSCode 詳細(xì)指南。
請(qǐng)選擇以下任一種方式輸入命令安裝依賴 :
- Windows 環(huán)境 打開(kāi) Cmd (開(kāi)始-運(yùn)行-CMD)。
- MacOS 環(huán)境 打開(kāi) Terminal (command+空格輸入Terminal)。
- 如果你用的是 VSCode編輯器 或 Pycharm,可以直接使用界面下方的Terminal.
pip install altair vega_datasets
2.基本使用
Altair 中的數(shù)據(jù)是圍繞 Pandas Dataframe 構(gòu)建的。
我們首先導(dǎo)入 Pandas 并創(chuàng)建一個(gè)簡(jiǎn)單的 DataFrame 以進(jìn)行可視化,a 列中有一個(gè)分類變量,b 列有一個(gè)數(shù)值變量:
import pandas as pd
data = pd.DataFrame({'a': list('CCCDDDEEE'),
'b': [2, 7, 4, 1, 2, 6, 8, 4, 7]})
Altair 中的基本對(duì)象是Chart,它將上述的數(shù)據(jù)作為單個(gè)參數(shù):
import altair as alt
chart = alt.Chart(data)
到目前為止,我們已經(jīng)定義了 Chart 對(duì)象,但是我們還沒(méi)有告訴圖表對(duì)數(shù)據(jù)做任何事情。接下來(lái)會(huì)出現(xiàn)。
有了這個(gè)圖表對(duì)象,我們現(xiàn)在可以指定我們希望如何可視化數(shù)據(jù),比如作為點(diǎn):
alt.Chart(data).mark_point()
然后對(duì)數(shù)據(jù)進(jìn)行編碼,比如指定 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對(duì)調(diào)一下:
alt.Chart(data).mark_bar().encode(
y='a',
x='average(b)'
)
除了點(diǎn)狀圖和柱狀圖,Altair 還支持幾十種圖表類型:
更多的圖表類型請(qǐng)?jiān)诠倬W(wǎng)查看:
https://altair-viz.github.io/gallery/index.html
3.高級(jí)使用
你可以給圖表自定義你喜歡的顏色和對(duì)應(yīng)的橫坐標(biāo)縱坐標(biāo)標(biāo)題:
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')
如果你希望能夠通過(guò)區(qū)間選擇數(shù)據(jù)點(diǎn)并計(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)動(dòng):
# 公眾號(hào):Python實(shí)用寶典 整合
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')
左邊圈起來(lái)的 Acceleration 數(shù)據(jù)點(diǎn),右邊會(huì)對(duì)應(yīng)顯示其 Miles_per_Gallon 數(shù)據(jù)點(diǎn):
除了這些,Altair還有更多的交互功能,比如選擇框拖動(dòng)、比例綁定、自動(dòng)響應(yīng)、表達(dá)式選擇等等。
-
數(shù)據(jù)分析
+關(guān)注
關(guān)注
2文章
1462瀏覽量
34264 -
python
+關(guān)注
關(guān)注
56文章
4812瀏覽量
85191 -
Altair
+關(guān)注
關(guān)注
0文章
18瀏覽量
10026 -
數(shù)據(jù)可視化
+關(guān)注
關(guān)注
0文章
473瀏覽量
10437
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
七款經(jīng)久不衰的數(shù)據(jù)可視化工具!
可視化MES系統(tǒng)軟件
15個(gè)玩轉(zhuǎn)數(shù)據(jù)可視化的技巧
三維可視化的應(yīng)用和優(yōu)勢(shì)
常見(jiàn)的幾種可視化介紹
經(jīng)驗(yàn)分享|BI數(shù)據(jù)可視化報(bào)表布局——容器
使用Python可視化數(shù)據(jù),機(jī)器人開(kāi)發(fā)編程
數(shù)據(jù)可視化的常用技術(shù)和并行與原位可視化方法分析
Charts.css是用于數(shù)據(jù)可視化的新的開(kāi)源框架
使用Python來(lái)收集、處理和可視化人口數(shù)據(jù)

一個(gè)非常棒的可視化庫(kù):PyG2Plot

評(píng)論