美文网首页
数据可视化库Altair的使用

数据可视化库Altair的使用

作者: 发条蛙 | 来源:发表于2017-11-06 19:56 被阅读0次

Altair是基于Vega-Lite的Python下的声明式统计可视化库。

通过Altair,可以将更多的时间花在理解数据及其含义上。Altair的API非常简单和友好,它基于Vega-Lite可视化语法构建,这使得可以使用少量的代码构造出优雅高效的可视化结果。

Altair的安装

最简单的安装方式无疑是使用pip进行安装:

pip install altair

Altair的使用

输入数据

Altair内部使用的数据以Pandas中的Dataframe格式存储,但有以下三种方式传入:

  1. 以Pandas的DataFrame格式传入;
    data = pd.DataFrame({'x': ['A', 'B', 'C', 'D', 'E'],
                         'y': [5, 3, 6, 7, 2]})
    
  2. 以Data对象传入;
    data = alt.Data(values=[{'x': 'A', 'y': 5},
                        {'x': 'B', 'y': 3},
                        {'x': 'C', 'y': 6},
                        {'x': 'D', 'y': 7},
                        {'x': 'E', 'y': 2}])
    
  3. 以指向csv或json文本的url传入;
    data = 'https://vega.github.io/vega-datasets/data/cars.json'
    

最终数据将作为Chart、LayeredChart或FacetedChart对象的第一个参数传入。

图形选择

定义好数据之后,首先需要做的就是选择需要显示的图形,即将数据以何种形式显示,目前Altair中有如下几种类型可供选择:

Mark Name Method Description
area mark_area() A filled area plot.
bar mark_bar() A bar plot.
circle mark_circle() A scatter plot with filled circles.
line mark_line() A line plot.
point mark_point() A scatter plot with configurable point shapes.
rule mark_rule() A vertical or horizontal line spanning the axis.
square mark_square() A scatter plot with filled squares.
text mark_text() A scatter plot with points represented by text
tick mark_tick() A vertical or horizontal tick mark.

编码方式

编码方式定义了图片显示的各种属性,如每个图片的位置,图片轴的属性等:

通道选择

位置通道定义了图片中位置相关的属性:

Channel Altair Class Description
column Column The column of a faceted plot
row Row The row of a faceted plot
x X The x-axis value
y Y The y-axis value

通道描述:

Channel Altair Class Description
color Color The color of the mark
opacity Opacity The opacity of the mark
shape Shape The shape of the mark
size Size The size of the mark

通道排序:

Channel Altair Class Description
order Order -
path Path -

通道域信息:

Channel Altair Class Description
text Text The text to display at each mark
detail Detail Additional level of detail for a grouping, without mapping to any particular channel
label Label

数据类型

定义数据如何在图片中显示:

Data Type Shorthand Code Description
quantitative Q a continuous real-valued quantity
ordinal O a discrete ordered quantity
nominal N a discrete unordered category
temporal T a time or date value

分类与聚合

Aggregate Description
sum Sum of values
mean Arithmetic mean of values
average Arithmetic mean of values
count Total number of values
distinct Number of distinct values
variance Variance of values
variancep ??
stdev Standard Deviation of values
stdevp ??
median Median of values
q1 First quartile of values
q3 Third quartile of values
modeskew ??
min Minimum value
max Maximum value
argmin Index of minimum value
argmax Index of maximum value
values ??
valid ??
missing ??

分类与聚合定义了数据在显示之前的处理方式:

Aggregate Description
sum Sum of values
mean Arithmetic mean of values
average Arithmetic mean of values
count Total number of values
distinct Number of distinct values
variance Variance of values
variancep ??
stdev Standard Deviation of values
stdevp ??
median Median of values
q1 First quartile of values
q3 Third quartile of values
modeskew ??
min Minimum value
max Maximum value
argmin Index of minimum value
argmax Index of maximum value
values ??
valid ??
missing ??

简写方式

一些常用的属性值可以简写:

Shorthand Equivalent long-form
x='name' X('name')
x='name:Q' X('name', type='quantitative')
x='sum(name)' X('name', aggregate='sum')
x='sum(name):Q' X('name', aggregate='sum', type='quantitative')

数据展示

Jupyter展示

本质上Altair只是将数据转换为Vega-Lite所使用的Json格式,可以通过如下方式查看:

print(chart.to_json(indent=2)) 

通常情况下都是在Jupyter中展示数据:

c = alt.Chart(...)
c

或者使用IPython的display()方法进行展示:

c = alt.Chart(...)
display(c)

或者:

c = alt.Chart(...)
c.display()

WebServer展示

通过Web方式展示如下:

chart.serve()   

存储为图片

目前Altair不支持存储为图片,要将其存储为图片则需要使用到Nodejs的包。

存储为HTML文件

具体代码如下:

chart.savechart('plot.html')  

其他表现形式

Altair图标可以表示为HTML、Python字典、Json对象和Altair的chart,在这些对象之间转换的函数有:

  • Chart.to_dict() / Chart.from_dict()
  • Chart.to_json() / Chart.from_json()
  • Chart.to_html()
  • Chart.to_python()

相关资源

  1. Altair源码:
    https://github.com/altair-viz/altair
  2. Altair文档:
    https://altair-viz.github.io/
  3. Vega-Lite源码:
    https://github.com/vega/vega-lite
  4. Vega-Lite文档:
    https://vega.github.io/vega-lite/

相关文章

网友评论

      本文标题:数据可视化库Altair的使用

      本文链接:https://www.haomeiwen.com/subject/nvwkmxtx.html