简体   繁体   中英

How to add legend to python altair

Can someone please help how to add legend for "Covid-19 Cases" and "Sales Volume"? In the chart, red is "COVID-19 Cases" and blue is the "Sales Volume". Thanks in advance.

在此处输入图像描述

# Create the chart
chart1 = alt.Chart(df_county).mark_line(color='red').encode(
    x=alt.X('Date', title='Date'),
    y=alt.Y('Cases', title='COVID-19 Cases'))


chart2 = alt.Chart(df_county).mark_line(color='blue').encode(
    x=alt.X('Date', title='Date'),
    y=alt.Y('Sale Volume', title='Sale Volume'))

chart = chart1 + chart2

There is no way to add a legend without specifying an encoding that the legend will represent. The issue is discussed further here https://github.com/altair-viz/altair/issues/984

See Adding legend to layerd chart in altair for a similar question.

You can either use a Fold Transform if both values have the same units, or you could introduce a Dual Y Axis without a legend.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM