bzm3r
bzm3r

Reputation: 4605

Change the source data of a chart?

Suppose we have:

import altair as alt
from vega_datasets import data
import sys

source = data.wheat()
chart = (
    alt.Chart(source)
    .mark_trail()
    .encode(x="year:O", y="wheat:Q", size="wheat:Q")
)
sys.displayhook(chart)

I'd like to swap out the source data for the chart for some other dataset, but leave everything else the same. How can I do so?

Upvotes: 0

Views: 12

Answers (1)

bzm3r
bzm3r

Reputation: 4605

Re-assign the data field on the chart:

import altair as alt
from vega_datasets import data
import sys

source = data.wheat()
chart = (
    alt.Chart(source)
    .mark_trail()
    .encode(x="year:O", y="wheat:Q", size="wheat:Q")
)
sys.displayhook(chart)

another_source = source[source.year > 1750]
chart.data = another_source
sys.displayhook(chart)

source another_source

Upvotes: 0

Related Questions