[PYTHON] Seaborn basics for beginners ② Histogram (distplot)

What is seaborn

A library for drawing Python graphs. .. Positioned as a wrapper function (inclusive program) of matplotlib, the most famous libra. In addition to being able to easily draw beautiful-looking graphs, it also has a certain amount of functions such as batch processing. Matplotlib is for detailed specification and drawing, and seaborn is for easy and beautiful.

Histogram (distplot)

The theme this time is a histogram. Use distplot to create the histogram. Please compare the difference in appearance and parameters with the following article using "matplotlib". ★ Create histogram with pandas basic matplotlib for beginners

Preparation

First, install the seaborn library with pip. For pip ?, click here ('https://qiita.com/Yanagawa_Yoshihisa/items/35e6f70a8411277282ce').

Import the library. Name seaborn`` sns and ʻimport`.

python


import seaborn as sns

I will try the sample with Titanic data. If you don't know Titanic, please check "kaggle Titanic". Create a dataframe with pandas.

python


dataframe = pd.read_csv('train.csv')

Basic creation

Use distplot to create the histogram. As an example, create a histogram by Age. In kde, whether or not to draw the density approximation function. It's difficult, so set it like a spell. Setting rug to True normalizes the Y-axis sum to 1. It's difficult, so set it like a spell.

python


sns.distplot(dataframe['Age'],kde=False, rug=False)

image.png

bins settings

Use bins to set the number of bars. Set 10 as an example.

python


sns.distplot(dataframe['Age'],bins = 10,kde=False, rug=False)

image.png

How many bins should I have? If you have any doubts, check out the Starges formula.

(Application) Overlapping multiple

It may not be very useful, but to stack it, you only have to write the syntax twice. Let's divide by Sex. Divide the data as a preparation.

python


malelist_m = dataframe['Sex'] == 'male'
malelist_f = dataframe['Sex'] == 'female'

Write the syntax continuously.

python


sns.distplot(dataframe[malelist_m]['Age'],bins = 10,kde=False, rug=False )
sns.distplot(dataframe[malelist_f]['Age'],bins = 10,kde=False, rug=False )

image.png If you want to use labels etc., you need to use the function of "matplotlib". that's all.

Finally

As a beginner can understand, we have summarized the necessary knowledge when implementing machine learning with Python as a simple article. The table of contents is here, so I hope you can refer to other articles as well.

Recommended Posts

Seaborn basics for beginners ② Histogram (distplot)
Seaborn basics for beginners ④ pairplot
Seaborn basics for beginners ③ Scatter plot (jointplot) * With histogram
Pandas basics for beginners ③ Histogram creation with matplotlib
Pandas basics for beginners ① Reading & processing
Pandas basics for beginners ⑧ Digit processing
[Must-see for beginners] Basics of Linux
Pandas basics summary link for beginners
Seaborn basics for beginners ① Aggregate graph of the number of data (Countplot)
Roadmap for beginners
Basics of pandas for beginners ② Understanding data overview
[Translation] NumPy Official Tutorial "NumPy: the absolute basics for beginners"
Spacemacs settings (for beginners)
Python basics ② for statement
python textbook for beginners
[Linux] Basics of authority setting by chmod for beginners
Dijkstra algorithm for beginners
OpenCV for Python beginners
[For beginners] Basics of Python explained by Java Gold Part 2
Pandas basics for beginners ④ Handling of date and time items
[For beginners] Basics of Python explained by Java Gold Part 1
Learning flow for Python beginners
[For beginners] kaggle exercise (merucari)
Linux distribution recommended for beginners
CNN (1) for image classification (for beginners)
Python3 environment construction (for beginners)
Overview of Docker (for beginners)
Python #function 2 for super beginners
Basic Python grammar for beginners
100 Pandas knocks for Python beginners
Python for super beginners Python #functions 1
Python #list for super beginners
~ Tips for beginners to Python ③ ~
[For Kaggle beginners] Titanic (LightGBM)
Reference resource summary (for beginners)
Linux command memorandum [for beginners]
Convenient Linux shortcuts (for beginners)