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.