user15155674
user15155674

Reputation:

Generate two legends via Altair

I would like to have two legends via Altair just like the picture below. enter image description here

I have created the legend of "Count of actors", but I don't know how to generate the other one. My code is below:

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

And the chart I created is like this: enter image description here

Upvotes: 0

Views: 346

Answers (1)

joelostblom
joelostblom

Reputation: 48929

This is the default behavior in Altair, but you have disabled the color legend. Change alt.Color('GENDER', legend=None) to alt.Color('GENDER').

Here is a modifed example for the Altair gallery with two legends:

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

Upvotes: 2

Related Questions