Altair mark shape

I wanna to draw scatter plot by altair to my dataframe.
In my dataframe (=df) if df[‘a’] = b, I draw circle point and if df[‘a’] = c, I draw triangle point.
I divided dataframe into column [‘a’] = b and column [‘a’] = c and drew two altairs. but I really want to make simple code.

So please tell me to one altair contains two shape (circle, triangle).

This is my code.
df_b = df[df[‘a’] == ‘b’]
df_c = df[df[‘a’] == ‘c’]

b = alt.Chart(df_b).mark_circle(size=50).encode(
x=alt.X(‘y:Q’, axis=None),
y=alt.X(‘x:Q’, axis=None)
)

c = alt.Chart(df_c).mark_point(shape=‘triangle’, filled=True, size=70).encode(
x=alt.X(‘y:Q’, axis=None),
y=alt.X(‘x:Q’, axis=None)
)

Hi @eric_l

You can achieve this by first adjusting your DataFrame structure so that you combine df_b and df_c into the same DataFrame. Now, you’ll have column a as in df[‘a’] that has values of ’b’ and ’c’ as in your example code snippet.

Next, in your altair code, make sure to add the color parameter and for this define column a as the column that it reads data from. For example this would be something like the following:


combined_chart = alt.Chart(df_c).mark_point(shape=‘triangle’, filled=True, size=70).encode(
x=alt.X(‘y:Q’, axis=None),
y=alt.X(‘x:Q’, axis=None),
color=‘a:O’
)

Hope this helps!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.