📜  如何通过 Altair 中的变量为散点图着色?

📅  最后修改于: 2022-05-13 01:55:40.836000             🧑  作者: Mango

如何通过 Altair 中的变量为散点图着色?

Altair是一个简单易用的Python统计可视化库。它提供了多种类型的可视化,从简单的条形图到箱形图等复合可视化。散点图是 Altair 库中最有用的可视化之一,用于双变量分析和查找数据集中两个数据列之间的关系。

入门

有时,简单的散点图不足以衡量数据集中变量之间的关系。更好的可视化将是两个定量变量/数据列之间相对于第三个变量的图。这第三个变量几乎总是一个名义或分类变量。我们可以使用第三个变量为散点图中的数据点着色。为散点图着色将帮助我们识别哪个数据点对应于第三个变量的哪个类别。

要为散点图着色,用户只需将数据集中的名义变量映射到颜色编码。

让我们通过一个例子来理解散点图着色的重要性:

Iris 数据集是数据科学中最流行的数据集之一,在大多数数据集库中都可用。该数据集记录了三个物种的鸢尾花。数据集中可用的数据列是 sepalLength、sepalWidth、petalLength、petalWidth 和物种。首先,我们将使用一个简单的散点图来可视化这个数据集,然后看看通过给这个散点图着色可以实现什么。

为了制作简单的散点图,我们使用来自 Vega _datasets库的 iris 数据集并将其传递给 Chart 对象并使用 mark_point() 方法。然后,我们将 x 和 y 轴编码映射为 sepalLength 和 petalLength 变量。

使用 iris 不着色的简单散点图:

Python3
# Python3 program to illustrate
# How to color a Scatter Plot
# using altair
  
# Importing altair and vega_datasets library
import altair as alt
from vega_datasets import data
  
# Selecting the iris dataset
iris = data.iris()
  
# Making the Scatter Plot
alt.Chart(iris).mark_point().encode(
  # Map the sepalLength to x-axis
    x = 'sepalLength',
  # Map the petalLength to y-axis
    y = 'petalLength',
)


Python3
# Python3 program to illustrate
# How to color a Scatter Plot
# using altair
  
# Importing altair and vega_datasets library
import altair as alt
from vega_datasets import data
  
# Selecting the iris dataset
iris = data.iris()
  
# Making the Scatter Plot
alt.Chart(iris).mark_point().encode(
  # Map the sepalLength to x-axis
    x = 'sepalLength',
  # Map the petalLength to y-axis
    y = 'petalLength',
  # Coloring the Scatter Plot
  # Map the species to color
    color = 'species'
)


Python3
# Python3 program to illustrate
# How to do custom mapping
# of colors to discrete values
# for scatter plot coloring
# using altair
  
# Importing altair and vega_datasets library
import altair as alt
from vega_datasets import data
  
# Selecting the cars dataset
cars = data.cars()
  
# Making two lists for
# values and colors resp.
dom = ['Europe', 'Japan', 'USA']
rng = ['red', 'green', 'black']
  
# Making the Scatter Plot
alt.Chart(cars).mark_point().encode(
    
    # Map Miles_per_Gallon to x-axis
    x='Miles_per_Gallon',
      
    # Map the Horsepower to y-axis
    y='Horsepower',
      
    # Coloring the Scatter Plot
    # using Origin variable and
    # custom colors
    color=alt.Color('Origin', scale=alt.
                    Scale(domain=dom, range=rng))
)


Python3
# Python3 program to illustrate
# How to select color schemes
# for scatter plot coloring
# using altair
  
# Importing altair and vega_datasets library
import altair as alt
from vega_datasets import data
  
# Selecting the cars dataset
cars = data.cars()
  
# Making the Scatter Plot
alt.Chart(cars).mark_point().encode(
    
    # Map Miles_per_Gallon to x-axis
    x='Miles_per_Gallon',
      
    # Map the Horsepower to y-axis
    y='Horsepower',
      
    # Coloring the Scatter Plot
    # using Origin variable and
    # color scheme
    color = alt.Color('Origin', scale=alt.
                      Scale(scheme = 'dark2'))
)


输出:

Iris 数据集的散点图,无需着色

如您所见,我们可以从这个散点图中推断出一组点与另一组点线性可分,但我们无法看到哪些数据点对应于哪些物种以及存在哪些类型的关系。为了使该图更具信息量,我们将使用物种变量为该散点图着色。

代码:

蟒蛇3

# Python3 program to illustrate
# How to color a Scatter Plot
# using altair
  
# Importing altair and vega_datasets library
import altair as alt
from vega_datasets import data
  
# Selecting the iris dataset
iris = data.iris()
  
# Making the Scatter Plot
alt.Chart(iris).mark_point().encode(
  # Map the sepalLength to x-axis
    x = 'sepalLength',
  # Map the petalLength to y-axis
    y = 'petalLength',
  # Coloring the Scatter Plot
  # Map the species to color
    color = 'species'
)

输出:

带有着色的鸢尾花数据集的散点图

Altair 会自动生成图例,指定哪种颜色代表颜色变量的哪个类别。通过查看彩色数据点,我们可以推断出setosa 物种的萼片长而花瓣短。杂色种有几乎相等的中等大小的花瓣和萼片,而弗吉尼亚种也有几乎相等但大的花瓣和萼片。

如您所见,我们可以通过为散点图着色来提取更多信息。

自定义颜色

如果您不喜欢 Altair 为散点图选择的颜色,您可以自定义颜色。可以使用 Color 类的 scale 参数更改默认颜色,通过将 Scale 类传递给 scale 参数。可用的自定义是:

  1. 颜色到离散值的自定义映射:对于自定义映射,我们使用 Scale 和 pass 列表的域和范围参数作为值和颜色。
  2. 配色方案:Vega 项目提供了许多配色方案。如果您喜欢深色,则可以使用“dark2”方案,如果类别超过 10 个,则可以使用“category20”方案。

示例 1:颜色到离散值的自定义映射:

蟒蛇3

# Python3 program to illustrate
# How to do custom mapping
# of colors to discrete values
# for scatter plot coloring
# using altair
  
# Importing altair and vega_datasets library
import altair as alt
from vega_datasets import data
  
# Selecting the cars dataset
cars = data.cars()
  
# Making two lists for
# values and colors resp.
dom = ['Europe', 'Japan', 'USA']
rng = ['red', 'green', 'black']
  
# Making the Scatter Plot
alt.Chart(cars).mark_point().encode(
    
    # Map Miles_per_Gallon to x-axis
    x='Miles_per_Gallon',
      
    # Map the Horsepower to y-axis
    y='Horsepower',
      
    # Coloring the Scatter Plot
    # using Origin variable and
    # custom colors
    color=alt.Color('Origin', scale=alt.
                    Scale(domain=dom, range=rng))
)

输出:

使用自定义颜色和值映射的汽车数据集散点图

示例 2(配色方案):

蟒蛇3

# Python3 program to illustrate
# How to select color schemes
# for scatter plot coloring
# using altair
  
# Importing altair and vega_datasets library
import altair as alt
from vega_datasets import data
  
# Selecting the cars dataset
cars = data.cars()
  
# Making the Scatter Plot
alt.Chart(cars).mark_point().encode(
    
    # Map Miles_per_Gallon to x-axis
    x='Miles_per_Gallon',
      
    # Map the Horsepower to y-axis
    y='Horsepower',
      
    # Coloring the Scatter Plot
    # using Origin variable and
    # color scheme
    color = alt.Color('Origin', scale=alt.
                      Scale(scheme = 'dark2'))
)

输出:

使用配色方案的汽车数据集散点图