!pip install matplotlib
!pip install seaborn
%matplotlib inline
!pip install pandas
import numpy as np
import os
import pandas as pd
import matplotlib.pyplot as plt
titanic = pd.read_csv('titanic.csv',sep=';')
print(titanic.head())
classe survie nom sexe age \ 0 1 1 Allen, Miss. Elisabeth Walton 2 29.0 1 1 1 Allison, Master. Hudson Trevor 1 1.0 2 1 0 Allison, Miss. Helen Loraine 2 2.0 3 1 0 Allison, Mr. Hudson Joshua Creighton 1 30.0 4 1 0 Allison, Mrs. Hudson Bessie Waldo Daniels 2 25.0 tarif 0 211.0 1 152.0 2 152.0 3 152.0 4 152.0
titanic.info()
<class 'pandas.core.frame.DataFrame'> RangeIndex: 1309 entries, 0 to 1308 Data columns (total 6 columns): classe 1309 non-null int64 survie 1309 non-null int64 nom 1309 non-null object sexe 1309 non-null int64 age 1046 non-null float64 tarif 1308 non-null float64 dtypes: float64(2), int64(3), object(1) memory usage: 61.4+ KB
titanic["survie"].value_counts()
0 809 1 500 Name: survie, dtype: int64
titanic.describe()
classe | survie | sexe | age | tarif | |
---|---|---|---|---|---|
count | 1309.000000 | 1309.000000 | 1309.000000 | 1046.000000 | 1308.000000 |
mean | 2.294882 | 0.381971 | 1.355997 | 29.897706 | 33.363150 |
std | 0.837836 | 0.486055 | 0.478997 | 14.414973 | 51.751529 |
min | 1.000000 | 0.000000 | 1.000000 | 0.000000 | 0.000000 |
25% | 2.000000 | 0.000000 | 1.000000 | 21.000000 | 8.000000 |
50% | 3.000000 | 0.000000 | 1.000000 | 28.000000 | 14.000000 |
75% | 3.000000 | 1.000000 | 2.000000 | 39.000000 | 31.000000 |
max | 3.000000 | 1.000000 | 2.000000 | 80.000000 | 512.000000 |
#titanic.hist(bins=10, figsize=(20,15))
#titanic.hist(column='age', bins=[0, 17, 80])
#titanic.query('survie==1').hist(figsize=(40,30))
#plt.show()
Seaborn est une librairie qui vient s'ajouter à Matplotlib, remplace certains réglages par défaut et fonctions, et lui ajoute de nouvelles fonctionnalités (les fonctions de Matplotlib ne sont pas faites pour interagir avec les Dataframes de Pandas) en utilisant toujours Matplotlib "sous le capot".
import seaborn as sns
import warnings
def plot_cat(data, x_axis, y_axis, hue):
#plt.figure()
sns.set(style="whitegrid")
sns.barplot(x=x_axis, y=y_axis, hue=hue, data=data)
sns.set_context("notebook", font_scale=1.6)
plt.legend(loc="upper right", fontsize="medium")
plt.show()
with warnings.catch_warnings(record=True):
plot_cat(titanic,"sexe", "survie", None)
plot_cat(titanic,"classe", "survie", "sexe")
plot_cat(titanic,"classe", "survie", None)