pythonvega-litealtair

How do I stop legends from being merged when vertically concatenating two plots?


Consider the following small example (based off of this gallery example]):

import altair as alt
from vega_datasets import data
import polars as pl

# add a column indicating the year associated with each date
source = pl.from_pandas(data.stocks()).with_columns(year=pl.col.date.dt.year())

# an MSFT specific plot
msft_plot = (
    alt.Chart(source.filter(pl.col.symbol.eq("MSFT")))
    .mark_line()
    .encode(x="date:T", y="price:Q", color="year:O")
)

# the original plot: https://altair-viz.github.io/gallery/line_chart_with_points.html
all_plot = (
    alt.Chart(source)
    .mark_line()
    .encode(x="date:T", y="price:Q", color="symbol:N")
)

msft_plot & all_plot

This produces the following output:

enter image description here

On the other hand, if I only plot all_plot:

enter image description here

How do I stop the legends being merged when I concatenate msft_plot & all_plot?


Solution

  • You can use (msft_plot & all_plot).resolve_scale(color='independent'):

    enter image description here

    More info about the resolve_ methods can be found in this section of the docs.