Seabornの基本操作をJupyter Labで書いてみた

## 第２４回
## Seaborn


```python
import seaborn as sns
%matplotlib inline
```


```python
import pandas as pd
df = pd.read_csv('train.csv')
df = df.dropna(subset=['Age'])
sns.distplot(df['Age'])
```




    <matplotlib.axes._subplots.AxesSubplot at 0x7fcbcdd7e3d0>




![png](output_2_1.png)



```python
sns.set()
sns.distplot(df['Age'],bins=50)
```




    <matplotlib.axes._subplots.AxesSubplot at 0x7fcbcdd05850>




![png](output_3_1.png)


## sns.jointplot()で２変数の分布を見る


```python
sns.jointplot(x='Age', y='Fare', data=df)
```




    <seaborn.axisgrid.JointGrid at 0x7fcbcdbe31d0>




![png](output_5_1.png)



```python
sns.jointplot(x='Age', y='Fare', data=df, kind='hex')
```




    <seaborn.axisgrid.JointGrid at 0x7fcbcdb2b890>




![png](output_6_1.png)


## (重要)sns.pairplot()で複数のカラムの分布を一発で表示


```python
sns.pairplot(df[['Age', 'Fare', 'Pclass', 'Survived']], hue='Survived', kind='scatter', plot_kws={'alpha': 0.5})
```

    /opt/anaconda3/lib/python3.7/site-packages/statsmodels/nonparametric/kde.py:487: RuntimeWarning: invalid value encountered in true_divide
      binned = fast_linbin(X, a, b, gridsize) / (delta * nobs)
    /opt/anaconda3/lib/python3.7/site-packages/statsmodels/nonparametric/kdetools.py:34: RuntimeWarning: invalid value encountered in double_scalars
      FAC1 = 2*(np.pi*bw/RANGE)**2





    <seaborn.axisgrid.PairGrid at 0x7fcbcda34990>




![png](output_8_2.png)


## 第２５回

## sns.barplot()  「棒グラフ」を作る


```python
import numpy as np
import pandas as pd
import seaborn as sns
%matplotlib inline
```


```python
df = pd.read_csv('train.csv')
df.head()
```




<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>PassengerId</th>
      <th>Survived</th>
      <th>Pclass</th>
      <th>Name</th>
      <th>Sex</th>
      <th>Age</th>
      <th>SibSp</th>
      <th>Parch</th>
      <th>Ticket</th>
      <th>Fare</th>
      <th>Cabin</th>
      <th>Embarked</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>0</td>
      <td>1</td>
      <td>0</td>
      <td>3</td>
      <td>Braund, Mr. Owen Harris</td>
      <td>male</td>
      <td>22.0</td>
      <td>1</td>
      <td>0</td>
      <td>A/5 21171</td>
      <td>7.2500</td>
      <td>NaN</td>
      <td>S</td>
    </tr>
    <tr>
      <td>1</td>
      <td>2</td>
      <td>1</td>
      <td>1</td>
      <td>Cumings, Mrs. John Bradley (Florence Briggs Th...</td>
      <td>female</td>
      <td>38.0</td>
      <td>1</td>
      <td>0</td>
      <td>PC 17599</td>
      <td>71.2833</td>
      <td>C85</td>
      <td>C</td>
    </tr>
    <tr>
      <td>2</td>
      <td>3</td>
      <td>1</td>
      <td>3</td>
      <td>Heikkinen, Miss. Laina</td>
      <td>female</td>
      <td>26.0</td>
      <td>0</td>
      <td>0</td>
      <td>STON/O2. 3101282</td>
      <td>7.9250</td>
      <td>NaN</td>
      <td>S</td>
    </tr>
    <tr>
      <td>3</td>
      <td>4</td>
      <td>1</td>
      <td>1</td>
      <td>Futrelle, Mrs. Jacques Heath (Lily May Peel)</td>
      <td>female</td>
      <td>35.0</td>
      <td>1</td>
      <td>0</td>
      <td>113803</td>
      <td>53.1000</td>
      <td>C123</td>
      <td>S</td>
    </tr>
    <tr>
      <td>4</td>
      <td>5</td>
      <td>0</td>
      <td>3</td>
      <td>Allen, Mr. William Henry</td>
      <td>male</td>
      <td>35.0</td>
      <td>0</td>
      <td>0</td>
      <td>373450</td>
      <td>8.0500</td>
      <td>NaN</td>
      <td>S</td>
    </tr>
  </tbody>
</table>
</div>




```python
sns.barplot(x='Survived', y='Age', data=df) #平均値
```




    <matplotlib.axes._subplots.AxesSubplot at 0x7fcbcdd7e390>




![png](output_13_1.png)



```python
sns.barplot(x='Survived', y='Age', data=df, estimator=np.median) #中央値
```




    <matplotlib.axes._subplots.AxesSubplot at 0x7fcbcce3a190>




![png](output_14_1.png)


## sns.countplot() データ数を比較


```python
sns.countplot(x='Sex', data=df, hue='Survived')
```




    <matplotlib.axes._subplots.AxesSubplot at 0x7fcbccfb0c10>




![png](output_16_1.png)


## sns.boxplot() カテゴリ別に値を比較する


```python
sns.boxplot(x='Pclass', y='Age', data=df)
```




    <matplotlib.axes._subplots.AxesSubplot at 0x7fcbccd82750>




![png](output_18_1.png)



```python
sns.boxplot(x='Pclass', y='Age', data=df, hue='Survived')
```




    <matplotlib.axes._subplots.AxesSubplot at 0x7fcbcc2a4ed0>




![png](output_19_1.png)


## sns.violonplot() データの分析を可視化できる


```python
sns.violinplot(x='Pclass', y='Age', data=df)
```




    <matplotlib.axes._subplots.AxesSubplot at 0x7fcbcc1e3ad0>




![png](output_21_1.png)



```python
sns.violinplot(x='Pclass', y='Age', data=df, hue=('Survived'))
```




    <matplotlib.axes._subplots.AxesSubplot at 0x7fcbcc107210>




![png](output_22_1.png)


## sns.swarmplot() 本当の分布を確認できる


```python
sns.swarmplot(x='Pclass', y='Age', data=df)
```




    <matplotlib.axes._subplots.AxesSubplot at 0x7fcbcc19a710>




![png](output_24_1.png)



```python
sns.swarmplot(x='Pclass', y='Age', data=df, size=4, hue='Survived')
```




    <matplotlib.axes._subplots.AxesSubplot at 0x7fcbc6e9fe50>




![png](output_25_1.png)


## 第２６回
## Heatmapを描写する

## df.corr()で相関関係を作る


```python
import pandas as pd
df = pd.read_csv('train.csv')
corr = df.corr()
corr
```




<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>PassengerId</th>
      <th>Survived</th>
      <th>Pclass</th>
      <th>Age</th>
      <th>SibSp</th>
      <th>Parch</th>
      <th>Fare</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>PassengerId</td>
      <td>1.000000</td>
      <td>-0.005007</td>
      <td>-0.035144</td>
      <td>0.036847</td>
      <td>-0.057527</td>
      <td>-0.001652</td>
      <td>0.012658</td>
    </tr>
    <tr>
      <td>Survived</td>
      <td>-0.005007</td>
      <td>1.000000</td>
      <td>-0.338481</td>
      <td>-0.077221</td>
      <td>-0.035322</td>
      <td>0.081629</td>
      <td>0.257307</td>
    </tr>
    <tr>
      <td>Pclass</td>
      <td>-0.035144</td>
      <td>-0.338481</td>
      <td>1.000000</td>
      <td>-0.369226</td>
      <td>0.083081</td>
      <td>0.018443</td>
      <td>-0.549500</td>
    </tr>
    <tr>
      <td>Age</td>
      <td>0.036847</td>
      <td>-0.077221</td>
      <td>-0.369226</td>
      <td>1.000000</td>
      <td>-0.308247</td>
      <td>-0.189119</td>
      <td>0.096067</td>
    </tr>
    <tr>
      <td>SibSp</td>
      <td>-0.057527</td>
      <td>-0.035322</td>
      <td>0.083081</td>
      <td>-0.308247</td>
      <td>1.000000</td>
      <td>0.414838</td>
      <td>0.159651</td>
    </tr>
    <tr>
      <td>Parch</td>
      <td>-0.001652</td>
      <td>0.081629</td>
      <td>0.018443</td>
      <td>-0.189119</td>
      <td>0.414838</td>
      <td>1.000000</td>
      <td>0.216225</td>
    </tr>
    <tr>
      <td>Fare</td>
      <td>0.012658</td>
      <td>0.257307</td>
      <td>-0.549500</td>
      <td>0.096067</td>
      <td>0.159651</td>
      <td>0.216225</td>
      <td>1.000000</td>
    </tr>
  </tbody>
</table>
</div>



## sns.heatmap()でHeatmapをplot


```python
sns.heatmap(corr)
```




    <matplotlib.axes._subplots.AxesSubplot at 0x7fcbc6df9850>




![png](output_30_1.png)



```python
sns.heatmap(corr, cmap='coolwarm', annot=True)
```




    <matplotlib.axes._subplots.AxesSubplot at 0x7fcbcd0b3290>




![png](output_31_1.png)


## データを俯瞰するときに役立つsns.heatmap()


```python
flights = sns.load_dataset('flights')
print(len(flights))
flights.head()
```

    144





<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>year</th>
      <th>month</th>
      <th>passengers</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>0</td>
      <td>1949</td>
      <td>January</td>
      <td>112</td>
    </tr>
    <tr>
      <td>1</td>
      <td>1949</td>
      <td>February</td>
      <td>118</td>
    </tr>
    <tr>
      <td>2</td>
      <td>1949</td>
      <td>March</td>
      <td>132</td>
    </tr>
    <tr>
      <td>3</td>
      <td>1949</td>
      <td>April</td>
      <td>129</td>
    </tr>
    <tr>
      <td>4</td>
      <td>1949</td>
      <td>May</td>
      <td>121</td>
    </tr>
  </tbody>
</table>
</div>




```python
# pivot_tableを作成
flights_pivot = flights.pivot_table(index='month', columns='year', values='passengers') 
flights_pivot
```




<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th>year</th>
      <th>1949</th>
      <th>1950</th>
      <th>1951</th>
      <th>1952</th>
      <th>1953</th>
      <th>1954</th>
      <th>1955</th>
      <th>1956</th>
      <th>1957</th>
      <th>1958</th>
      <th>1959</th>
      <th>1960</th>
    </tr>
    <tr>
      <th>month</th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>January</td>
      <td>112</td>
      <td>115</td>
      <td>145</td>
      <td>171</td>
      <td>196</td>
      <td>204</td>
      <td>242</td>
      <td>284</td>
      <td>315</td>
      <td>340</td>
      <td>360</td>
      <td>417</td>
    </tr>
    <tr>
      <td>February</td>
      <td>118</td>
      <td>126</td>
      <td>150</td>
      <td>180</td>
      <td>196</td>
      <td>188</td>
      <td>233</td>
      <td>277</td>
      <td>301</td>
      <td>318</td>
      <td>342</td>
      <td>391</td>
    </tr>
    <tr>
      <td>March</td>
      <td>132</td>
      <td>141</td>
      <td>178</td>
      <td>193</td>
      <td>236</td>
      <td>235</td>
      <td>267</td>
      <td>317</td>
      <td>356</td>
      <td>362</td>
      <td>406</td>
      <td>419</td>
    </tr>
    <tr>
      <td>April</td>
      <td>129</td>
      <td>135</td>
      <td>163</td>
      <td>181</td>
      <td>235</td>
      <td>227</td>
      <td>269</td>
      <td>313</td>
      <td>348</td>
      <td>348</td>
      <td>396</td>
      <td>461</td>
    </tr>
    <tr>
      <td>May</td>
      <td>121</td>
      <td>125</td>
      <td>172</td>
      <td>183</td>
      <td>229</td>
      <td>234</td>
      <td>270</td>
      <td>318</td>
      <td>355</td>
      <td>363</td>
      <td>420</td>
      <td>472</td>
    </tr>
    <tr>
      <td>June</td>
      <td>135</td>
      <td>149</td>
      <td>178</td>
      <td>218</td>
      <td>243</td>
      <td>264</td>
      <td>315</td>
      <td>374</td>
      <td>422</td>
      <td>435</td>
      <td>472</td>
      <td>535</td>
    </tr>
    <tr>
      <td>July</td>
      <td>148</td>
      <td>170</td>
      <td>199</td>
      <td>230</td>
      <td>264</td>
      <td>302</td>
      <td>364</td>
      <td>413</td>
      <td>465</td>
      <td>491</td>
      <td>548</td>
      <td>622</td>
    </tr>
    <tr>
      <td>August</td>
      <td>148</td>
      <td>170</td>
      <td>199</td>
      <td>242</td>
      <td>272</td>
      <td>293</td>
      <td>347</td>
      <td>405</td>
      <td>467</td>
      <td>505</td>
      <td>559</td>
      <td>606</td>
    </tr>
    <tr>
      <td>September</td>
      <td>136</td>
      <td>158</td>
      <td>184</td>
      <td>209</td>
      <td>237</td>
      <td>259</td>
      <td>312</td>
      <td>355</td>
      <td>404</td>
      <td>404</td>
      <td>463</td>
      <td>508</td>
    </tr>
    <tr>
      <td>October</td>
      <td>119</td>
      <td>133</td>
      <td>162</td>
      <td>191</td>
      <td>211</td>
      <td>229</td>
      <td>274</td>
      <td>306</td>
      <td>347</td>
      <td>359</td>
      <td>407</td>
      <td>461</td>
    </tr>
    <tr>
      <td>November</td>
      <td>104</td>
      <td>114</td>
      <td>146</td>
      <td>172</td>
      <td>180</td>
      <td>203</td>
      <td>237</td>
      <td>271</td>
      <td>305</td>
      <td>310</td>
      <td>362</td>
      <td>390</td>
    </tr>
    <tr>
      <td>December</td>
      <td>118</td>
      <td>140</td>
      <td>166</td>
      <td>194</td>
      <td>201</td>
      <td>229</td>
      <td>278</td>
      <td>306</td>
      <td>336</td>
      <td>337</td>
      <td>405</td>
      <td>432</td>
    </tr>
  </tbody>
</table>
</div>




```python
sns.heatmap(flights_pivot)
```




    <matplotlib.axes._subplots.AxesSubplot at 0x7fcbc5baabd0>




![png](output_35_1.png)


## 第２7回
## sns.set()で基本styleを変更

## context引数で用途を指定


```python
import pandas as pd
import seaborn as sns
%matplotlib inline

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

sns.set(context=('poster'))
df = df.dropna(subset=['Age'])
sns.distplot(df['Age'])
```




    <matplotlib.axes._subplots.AxesSubplot at 0x7fcbc568f690>




![png](output_38_1.png)


## style引数でグラフ全体のstyleを指定


```python
sns.set_style(style='whitegrid') #背景色を変更
sns.distplot(df['Age'])
```




    <matplotlib.axes._subplots.AxesSubplot at 0x7fcbc563d850>




![png](output_40_1.png)


## palette引数で色を指定


```python
sns.set(palette='bright')
sns.violinplot(x='Pclass', y='Age', data=df)
```




    <matplotlib.axes._subplots.AxesSubplot at 0x7fcbc5472850>




![png](output_42_1.png)


## sns.despine()で軸や枠を落とす


```python
sns.set(palette='bright' ,style='ticks') ##style='ticks'でplotに軸
sns.violinplot(x='Pclass', y='Age', data=df)
```




    <matplotlib.axes._subplots.AxesSubplot at 0x7fcbc53ed810>




![png](output_44_1.png)



```python
sns.set(palette='bright' )
sns.violinplot(x='Pclass', y='Age', data=df)
sns.despine()
```


![png](output_45_0.png)


## matplotlibと同様pltモジュールでいろいろなことができる


```python
import matplotlib.pyplot as plt
plt.figure(figsize=(10, 5))
sns.distplot(df['Age'])
```




    <matplotlib.axes._subplots.AxesSubplot at 0x7fcbc52d8750>




![png](output_47_1.png)



```python
sns.distplot(df['Age'])
plt.savefig('seaborn_sample.png')
```


![png](output_48_0.png)



```python

```