通过 Altair 生成两个图例

时间:2021-02-14 19:37:34

标签: python python-3.x altair

我想通过 Altair 拥有两个传说,如下图所示。 enter image description here

我已经创建了“演员数”的传奇,但我不知道如何生成另一个。我的代码如下:

plot = base.mark_circle(
   opacity=0.8,
   stroke='black',
   strokeWidth=1
).encode(
   alt.X('TYPE:O'),
   alt.Y('index:N',
          sort= movies_order
          ),
   alt.Size('count(index):Q',
   scale=alt.Scale(range=[0,4500]),
   legend=alt.Legend(title='Count of actors', symbolFillColor='white')),
   alt.Color('GENDER', legend=None)
   #complete this
).properties(
   width=350,
   height=880

而我创建的图表是这样的: enter image description here

1 个答案:

答案 0 :(得分:2)

这是 Altair 中的默认行为,但您已禁用颜色图例。将 alt.Color('GENDER', legend=None) 更改为 alt.Color('GENDER')

这是 Altair 画廊的修改示例,其中包含两个图例:

import altair as alt
from vega_datasets import data

source = data.cars()

alt.Chart(source).mark_circle().encode(
    x='Horsepower',
    y='Miles_per_Gallon',
    color='Origin',
    size='Cylinders')

enter image description here