如何将图例添加到 python altair

How to add legend to python altair

有人可以帮助如何为“Covid-19 案例”和“销售量”添加图例吗? 在图表中,红色是“COVID-19 Cases”,蓝色是“Sales Volume”。提前致谢。

# 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

如果不指定图例将表示的编码,则无法添加图例。此处进一步讨论了该问题 https://github.com/altair-viz/altair/issues/984

类似问题。

您可以使用不带图例的 Fold Transform if both values have the same units, or you could introduce a Dual Y Axis