How to color code even methods that do not have hue as an argument in Seaborn

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

python: 3.7.4 seaborn: 0.9.0

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')

countplot.png In this way, it draws a graph by color-coding each category of'Survived'.

When not having hue as an argument

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()

distplot.png 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. (For more information about FacetGrid, please refer to Plot and visualize with Seaborn FacetGrid etc.)

Summary

If you want to color-code graphs that do not have hue as an argument, such as distplot and kdeplot, use FacetGrid.

Recommended Posts

How to color code even methods that do not have hue as an argument in Seaborn
How to hide warnings that do not affect execution in Jupyter Notebook
[Python] How to do PCA in Python
How to run TensorFlow 1.0 code in 2.0
How to do R chartr () in Python
How to choose a Seaborn color palette
How to do Server-Sent Events in Django
How to change editor color in PyCharm