Skip to content
Advertisement

Sns Scatter plot with differet color coding and different markers

I would like to create a Seaborn scatter-plot, using the following dataframe:

df = pd.DataFrame({'A':[1,2,3,4],'B':[2,4,6,8],'C':['y','y','n','n'],'D':[1,1,2,2]})

In my graph A should be the x-variable and B the y-variable. Furthermore I would like to color based on column D. Finally, when C='y' the marker should be open-faced (no facecolor) and when C='n' the marker should have a closed. My original idea was to use the hue and style parameter:

sns.scatterplot(x='A', y='B',
                        data=df, hue='D',style ='C')

However, I did not manage to obtain the graph I am looking for. Could somebody help me with this? Thank you in advance.

Advertisement

Answer

One cannot specify entire marker styles (so 'marker' and 'fillstyle' keys in your case) for matplotlib yet. Have a look on the answer to this post.

So the only thing left for you is to use different markers right away and specify them (as list or dictionary)

sns.scatterplot(data=df, x='A', y='B', hue='D', style='C', markers=['o', 's'])
plt.show()

Apparently, it is very hard to even create non-filled markers in seaborn, as this post explains. The only option is to do some matplotlib-seaborn-hybrid thing… So if you accept to plot things twice onto the same axis (one for a filled marker and one for the unfilled markers), you still have to dig yourself into the quirks of seaborn…

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement