- #43 Use categorical variable for color
- #43 Use categorical variable for color
- #43 Use categorical variable for color
- #43 Use categorical variable for color
Once you understood how to make a basic scatterplot with seaborn and how to custom shapes and color, you probably want the color corresponds to a categorical variable (a group). This is possible using the hue argument: it’s here that you must specify the column to use to map the color.
- Map a color per group
-
# library & dataset import seaborn as sns df = sns.load_dataset('iris') # Use the 'hue' argument to provide a factor variable sns.lmplot( x="sepal_length", y="sepal_width", data=df, fit_reg=False, hue='species', legend=False) # Move the legend to an empty part of the plot plt.legend(loc='lower right') #sns.plt.show()
- Map a marker per group
-
# library & dataset import seaborn as sns df = sns.load_dataset('iris') # give a list to the marker argument sns.lmplot( x="sepal_length", y="sepal_width", data=df, fit_reg=False, hue='species', legend=False, markers=["o", "x", "1"]) # Move the legend to an empty part of the plot plt.legend(loc='lower right') #sns.plt.show()
- Use another palette
-
Several palettes are available, for example: deep, muted, bright, pastel, dark, colorblind. See a complete list here TODO
# library & dataset import seaborn as sns df = sns.load_dataset('iris') # Use the 'palette' argument sns.lmplot( x="sepal_length", y="sepal_width", data=df, fit_reg=False, hue='species', legend=False, palette="Set2") # Move the legend to an empty part of the plot plt.legend(loc='lower right') #sns.plt.show()
- Control color of each group
-
# library & dataset import seaborn as sns df = sns.load_dataset('iris') # Provide a dictionary to the palette argument sns.lmplot( x="sepal_length", y="sepal_width", data=df, fit_reg=False, hue='species', legend=False, palette=dict(setosa="#9b59b6", virginica="#3498db", versicolor="#95a5a6")) # Move the legend to an empty part of the plot plt.legend(loc='lower right') #sns.plt.show()
Note that this online course offers a whole chapter on seaborn. It might help you if you are a Seaborn newbie.