Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using different colors AND shapes in legend [Seaborn, Python]

I've read through examples on how to use different shapes and/or colors to separate data in a Seaborn plot. However, it seems that color and shapes are tied together, to show a separate variable. For instance, in the following script (borrowed from the link above) it seems like you can only use green down arrows and grey up arrows:

g = sns.FacetGrid(tips, col="sex", hue="time", palette=pal,
                  hue_order=["Dinner", "Lunch"],
                  hue_kws=dict(marker=["^", "v"]))
g = (g.map(plt.scatter, "total_bill", "tip", **kws).add_legend())

Is it possible to show, say, green up arrows and grey up arrows, as well as green down arrows and grey down arrows?

I've tried to define a dictionary for col in a similar fashion to what is being done for hue, but I'm still trying to wrap my head around this.

like image 248
AaronJPung Avatar asked Feb 05 '23 18:02

AaronJPung


1 Answers

I assume that you are taking advantage of some of the features of the sns.FacetGrid function. If you are just trying to make a simple scatter plot, then using plt.scatter directly is probably more straightforward.

Anyway, it seems possible to use whatever colors and symbols that you want in the FacetGrid example. I'm not a Seaborn pro, but here is one way to accomplish this:

import seaborn as sns
import matplotlib.pyplot as plt

tips = sns.load_dataset("tips")
kws = dict(s=50, linewidth=.5, edgecolor="w")
pal = ['red', 'green', 'blue', 'red', 'green', 'blue',]
g = sns.FacetGrid(tips, col="sex", hue="size", palette=pal, hue_kws=dict(marker=["^", "^", "^", "v", "v", "v"]))
g = (g.map(plt.scatter, "total_bill", "tip", **kws).add_legend())

plt.show()

example output

like image 153
DanHickstein Avatar answered Feb 08 '23 00:02

DanHickstein