mirror of
https://github.com/LCTT/TranslateProject.git
synced 2025-02-25 00:50:15 +08:00
PRF
@silentdawn-zz
This commit is contained in:
parent
e6321073eb
commit
3474b48863
@ -1,18 +1,20 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: ()
|
||||
[#]: reviewer: ( )
|
||||
[#]: translator: (silentdawn-zz)
|
||||
[#]: reviewer: (wxy)
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
[#]: subject: (Concise data plotting in Python with Altair)
|
||||
[#]: via: (https://opensource.com/article/20/6/altair-python)
|
||||
[#]: author: (Shaun Taylor-Morgan https://opensource.com/users/shaun-taylor-morgan)
|
||||
|
||||
Python 下使用 Altair 数据制图简明教程
|
||||
Python 下使用 Altair 数据制图
|
||||
======
|
||||
Altair 作为一个 Python 数据制图库,提供了优雅的接口及自有的绘图语言。
|
||||
![metrics and data shown on a computer screen][1]
|
||||
|
||||
Python 中的 [绘图库][2] 提供了呈现数据的多种方式,可以满足你不同的偏好,如灵活性、布局、易用性,或者特殊的类型。
|
||||
> Altair 作为一个 Python 数据制图库,提供了优雅的接口及自有的绘图语言。
|
||||
|
||||

|
||||
|
||||
Python 中的 [绘图库][2] 提供了呈现数据的多种方式,可以满足你不同的偏好,如灵活性、布局、易用性,或者特殊的风格。
|
||||
|
||||
和其它方式相比,我发现,Altair 提供的是一种不同的解决方案,且总体而言使用起来更为简单。得益于声明式的绘图语言 [Vega][3],Altair 拥有一套优雅的接口,可以直接定义要绘的图应该是什么样子,而不是通过写一大堆循环和条件判断去一步步构建。
|
||||
|
||||
@ -20,17 +22,14 @@ Python 中的 [绘图库][2] 提供了呈现数据的多种方式,可以满足
|
||||
|
||||
我通过绘制同一个多柱状图比较了多个 Python 绘图库的差异。正式开始之前,你需要将你的 Python 环境调整到能运行下面代码的状态。具体就是:
|
||||
|
||||
* 安装最新版的 Python( [Linux][4]、[Mac][5] 和 [Windows][6] 系统下的安装方法)
|
||||
* 确认该版本 Python 可以运行本教程所使用的库
|
||||
|
||||
|
||||
* 安装最新版的 Python( [Linux][4]、[Mac][5] 和 [Windows][6] 系统下的安装方法)
|
||||
* 确认该版本 Python 可以运行本教程所使用的库
|
||||
|
||||
演示用数据可从网络下载,并且可以用 pandas 直接导入:
|
||||
|
||||
|
||||
```
|
||||
import pandas as pd
|
||||
df = pd.read_csv('<https://anvil.works/blog/img/plotting-in-python/uk-election-results.csv>')
|
||||
df = pd.read_csv('https://anvil.works/blog/img/plotting-in-python/uk-election-results.csv')
|
||||
```
|
||||
|
||||
准备开始吧。为了做个比较,先看下面这个用 [Matplotlib][7] 做的图:
|
||||
@ -41,7 +40,6 @@ df = pd.read_csv('<https://anvil.works/blog/img/plotting-in-python/uk-election-r
|
||||
|
||||
使用 Altair 绘制相似的图,代码如下:
|
||||
|
||||
|
||||
```
|
||||
import altair as alt
|
||||
|
||||
@ -55,7 +53,7 @@ df = pd.read_csv('<https://anvil.works/blog/img/plotting-in-python/uk-election-r
|
||||
chart.save('altair-elections.html')
|
||||
```
|
||||
|
||||
真是简洁多了!与 [Seaborn][9] 类似,Altair 所用数据的组织形式是每个变量一列(即 [数据列][10] )。这种方式下可以将每个变量映射到图的一个属性上—— Altair 称之为”通道“。在上例中,我们期望每个 “党派” 在 `x` 轴上显示为一组图柱, 其 “席位” 显示在 `y` 轴,且将图柱按照 “年份” 分开为 “列”。我们还想根据 “党派” 给图柱使用不同的 “颜色”。用语言表述需求的话就是上面这个样子,而这也正是代码所要表述的!
|
||||
真是简洁多了!与 [Seaborn][9] 类似,Altair 所用数据的组织形式是每个变量一列(即 [数据列][10] )。这种方式下可以将每个变量映射到图的一个属性上 —— Altair 称之为“通道”。在上例中,我们期望每个 “党派” 在 `x` 轴上显示为一组图柱, 其 “席位” 显示在 `y` 轴,且将图柱按照 “年份” 分开为 “列”。我们还想根据 “党派” 给图柱使用不同的 “颜色”。用语言表述需求的话就是上面这个样子,而这也正是代码所要表述的!
|
||||
|
||||
现在把图画出来:
|
||||
|
||||
@ -63,7 +61,7 @@ df = pd.read_csv('<https://anvil.works/blog/img/plotting-in-python/uk-election-r
|
||||
|
||||
### 调整样式
|
||||
|
||||
这和我们期待的效果有点接近了。与 Matplotlib 方案相比,主要区别在于 Altair 方案中,每个 `year` 组显示的时候,内部之间都有个小空白——这不是问题,这只是 Altair 多柱状图显示的一个特性。
|
||||
这和我们期待的效果有点接近了。与 Matplotlib 方案相比,主要区别在于 Altair 方案中,每个 `year` 组显示的时候,内部之间都有个小空白 —— 这不是问题,这只是 Altair 多柱状图显示的一个特性。
|
||||
|
||||
所以说呢,还需要对显示样式再做一些改进。
|
||||
|
||||
@ -73,7 +71,7 @@ df = pd.read_csv('<https://anvil.works/blog/img/plotting-in-python/uk-election-r
|
||||
|
||||
|
||||
```
|
||||
` df['year'] = df['year'].astype(str)`
|
||||
df['year'] = df['year'].astype(str)
|
||||
```
|
||||
|
||||
#### 指定数据排序方法
|
||||
@ -104,7 +102,6 @@ df = pd.read_csv('<https://anvil.works/blog/img/plotting-in-python/uk-election-r
|
||||
|
||||
最后,我们还想自己指定图柱的颜色。Altair 允许建立 `domain` 中数值与 `range` 中颜色的映射来实现所需功能,太贴心了:
|
||||
|
||||
|
||||
```
|
||||
cmap = {
|
||||
'Conservative': '#0343df',
|
||||
@ -121,8 +118,7 @@ df = pd.read_csv('<https://anvil.works/blog/img/plotting-in-python/uk-election-r
|
||||
|
||||
#### 样式调整后的最终代码
|
||||
|
||||
应用上述样式调整之后,代码看起来不那么悦目了,但我们仍然是用声明的方式实现的,这正式 Altair 如此有弹性的原因所在。实现过程中,仍然是使用的异于显示数据的独立变量来分离图中不同属性的,而不是像在 Matplotlib 中那样直接对显示数据做复杂的操作。唯一的不同是,我们的变量名字封装在类似 `alt.X()` 的对象中,从而实现对显示效果的控制:
|
||||
|
||||
应用上述样式调整之后,代码看起来不那么悦目了,但我们仍然是用声明的方式实现的,这正是 Altair 如此有弹性的原因所在。实现过程中,仍然是使用的异于显示数据的独立变量来分离图中不同属性的,而不是像在 Matplotlib 中那样直接对显示数据做复杂的操作。唯一的不同是,我们的变量名字封装在类似 `alt.X()` 的对象中,从而实现对显示效果的控制:
|
||||
|
||||
```
|
||||
import altair as alt
|
||||
@ -155,13 +151,11 @@ df = pd.read_csv('<https://anvil.works/blog/img/plotting-in-python/uk-election-r
|
||||
|
||||
![The Altair plot with our custom styling][12]
|
||||
|
||||
### **结论**
|
||||
### 结论
|
||||
|
||||
尽管在代码数量上,使用 Altair 绘图没有表现出优势,但它的声明式绘图语言使得对图层的操控更为精密,这是我比较欣赏的。Altair 还提供了清晰而独立的方式来调校显示样式,这使得 相关代码与绘图的代码块分离开来。Altair 确实是使用 Python 绘图时又一个很棒的工具库。
|
||||
|
||||
\---
|
||||
|
||||
_本文首次发布于 [这里][13],蒙允编辑后再次发布_
|
||||
本文首次发布于 [这里][13],蒙允编辑后再次发布。
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
@ -169,15 +163,15 @@ via: https://opensource.com/article/20/6/altair-python
|
||||
|
||||
作者:[Shaun Taylor-Morgan][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[silentdawn-zz](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
译者:[silentdawn-zz](https://github.com/silentdawn-zz)
|
||||
校对:[wxy](https://github.com/wxy)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://opensource.com/users/shaun-taylor-morgan
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/metrics_data_dashboard_system_computer_analytics.png?itok=oxAeIEI- (metrics and data shown on a computer screen)
|
||||
[2]: https://opensource.com/article/20/4/plot-data-python
|
||||
[2]: https://linux.cn/article-12327-1.html
|
||||
[3]: https://vega.github.io/vega/
|
||||
[4]: https://opensource.com/article/20/4/install-python-linux
|
||||
[5]: https://opensource.com/article/19/5/python-3-default-mac
|
||||
|
Loading…
Reference in New Issue
Block a user