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.
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
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')
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)
Use bins
to set the number of bars. Set 10 as an example.
python
sns.distplot(dataframe['Age'],bins = 10,kde=False, rug=False)
How many bins should I have? If you have any doubts, check out the Starges formula.
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 )
If you want to use labels etc., you need to use the function of "matplotlib". that's all.
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