[python]How to color code even methods that do not have hue as an argument in Seaborn
- M.R

- Aug 21, 2021
- 1 min read
Introduction
A library seaborn that can visualize data in a beautiful graph. Among them, in some methods, if you specify a category in the argument hue, it will color-code each category and draw a graph.
However, some methods do not take this hue as an argument. Even in such a case, I will introduce how to color-code each category.
Environment is python: 3.7.4 seaborn: 0.9.0
What is the argument hue
First, let's use hue with the countplot method. The data is titanic.
import pandas as pd
import seaborn as sns
data=pd.read_csv("train.csv")
sns.countplot(x='Embarked', data=data, hue='Survived')
In this way, it draws a graph by color-coding each category of 'Survived'.
If no argument hue
Well, the main subject is from here. For example, the seaborn distplot method does not have a hue argument. (seaborn.distplot)
But sometimes you want to see the distribution for each category. FacetGrid can be used in such cases.
g=sns.FacetGrid(data, hue='Survived', size=5)
g.map(sns.distplot, "Fare")
g.add_legend()
In this way, if you specify it in the hue of the FacetGrid method, you can handle graphs that do not have hue as an argument (reference).
Summary
Use FacetGrid if you want to color-code graphs that do not have hue as an argument, such as distplot and kdeplot.






Comments