How to make chart template for use with different data sources #2826
-
MWEimport altair as alt
import numpy as np
import pandas as pd
x, y = np.meshgrid(range(-5, 5), range(-5, 5))
z1 = x ** 2 + y ** 2
z2 = x * 2 + y * 2
df1 = pd.DataFrame({'x': x.ravel(),
'y': y.ravel(),
'z': z1.ravel()})
df2 = pd.DataFrame({'x': x.ravel(),
'y': y.ravel(),
'z': z2.ravel()})
template = alt.Chart().mark_rect().encode(
x='x:O',
y='y:O',
color='z:Q'
)
df1_chart = template.data(df1)
df2_chart = template.data(df2) Modifed from this example. QuestionI'm looking for something semantically equivalent to the last two lines, but syntactically correct. I am looking for an altair way to do this; I understand I could wrap the chart code in a function. |
Beta Was this translation helpful? Give feedback.
Answered by
joelostblom
Jan 12, 2023
Replies: 1 comment 1 reply
-
You can either use the properties method: template.properties(data=df1) Or directly modify the object template['data'] = df1
template The first would likely be to prefer in most cases since it uses a method rather than changing the object manually |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
mcp292
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can either use the properties method:
Or directly modify the object
The first would likely be to prefer in most cases since it uses a method rather than changing the object manually