top of page

[python]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 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.

Recent Posts

See All

[Python] Output pandas.DataFrame as json

Summary Data analysis is performed using python. The analysis itself is performed using pandas, and the final results are stored in pandas.DataFrame format. I want to output this result to a file in j

[Python] Conditionally fitting

Overview If you want to do fitting, you can do it with scipy.optimize.leastsq etc. in python. However, when doing fitting, there are many cases where you want to condition the fitting parameters. For

Comments


Let's do our best with our partner:​ ChatReminder

iphone6.5p2.png

It is an application that achieves goals in a chat format with partners.

google-play-badge.png
Download_on_the_App_Store_Badge_JP_RGB_blk_100317.png

Let's do our best with our partner:​ ChatReminder

納品:iPhone6.5①.png

It is an application that achieves goals in a chat format with partners.

google-play-badge.png
Download_on_the_App_Store_Badge_JP_RGB_blk_100317.png

Theme diary: Decide the theme and record for each genre

It is a diary application that allows you to post and record with themes and sub-themes for each genre.

google-play-badge.png
Download_on_the_App_Store_Badge_JP_RGB_blk_100317.png
bottom of page