Home > Développement > Python > Seaborn

Seaborn

import seaborn as sns
tips = sns.load_dataset('tips')
tips.head()
total_bill tip sex smoker day time size
0 16.99 1.01 Female No Sun Dinner 2
1 10.34 1.66 Male No Sun Dinner 3
2 21.01 3.50 Male No Sun Dinner 3
3 23.68 3.31 Male No Sun Dinner 2
4 24.59 3.61 Female No Sun Dinner 4
sns.barplot(x='sex', y='total_bill', data=tips)
/usr/local/lib/python3.6/dist-packages/scipy/stats/stats.py:1713: FutureWarning: Using a non-tuple sequence for multidimensional indexing is deprecated; use `arr[tuple(seq)]` instead of `arr[seq]`. In the future this will be interpreted as an array index, `arr[np.array(seq)]`, which will result either in an error or a different result.
  return np.add.reduce(sorted[indexer] * weights, axis=axis) / sumval
<matplotlib.axes._subplots.AxesSubplot at 0x7f9a3ea9eb70>

png

import numpy as np
sns.barplot(x='sex', y='total_bill', data=tips, estimator=np.std)
<matplotlib.axes._subplots.AxesSubplot at 0x7f9a3c9b99b0>

png

sns.boxplot(x='day', y='total_bill', data=tips, palette='rainbow')
<matplotlib.axes._subplots.AxesSubplot at 0x7f9a3c97a3c8>

png

sns.boxplot(data=tips, palette='rainbow', orient='h')
<matplotlib.axes._subplots.AxesSubplot at 0x7f9a3c89af98>

png

sns.boxplot(x='day', y='total_bill', hue='smoker', data=tips, palette='rainbow')
<matplotlib.axes._subplots.AxesSubplot at 0x7f9a3c881470>

png

sns.violinplot(x='day', y='total_bill', data=tips, palette='rainbow') # densité
<matplotlib.axes._subplots.AxesSubplot at 0x7f9a3c78d4a8>

png

sns.violinplot(x='day', y='total_bill', data=tips, hue='sex', palette='rainbow')
<matplotlib.axes._subplots.AxesSubplot at 0x7f9a3c7c6898>

png

sns.violinplot(x='day', y='total_bill', data=tips, hue='sex', split=True, palette='rainbow'
<matplotlib.axes._subplots.AxesSubplot at 0x7f9a3a610f28>

png

sns.stripplot(x='day', y='total_bill', data=tips)
<matplotlib.axes._subplots.AxesSubplot at 0x7f9a3a5ab320>

png

sns.stripplot(x='day', y='total_bill', data=tips, jitter=False)

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

png

sns.stripplot(x='day', y='total_bill', data=tips, hue='sex', palette='Set1')
<matplotlib.axes._subplots.AxesSubplot at 0x7f9a3a349c50>

png

sns.swarmplot(x='day', y='total_bill', data=tips)
<matplotlib.axes._subplots.AxesSubplot at 0x7f9a3a2ed828>

png

sns.swarmplot(x='day', y='total_bill', data=tips, hue='sex', dodge=True)
<matplotlib.axes._subplots.AxesSubplot at 0x7f9a3a5164a8>

png

sns.set_context('poster',font_scale=1)
sns.violinplot(x='day', y='total_bill', data=tips, palette='rainbow')
sns.swarmplot(x='day', y='total_bill', data=tips, size=1)
<matplotlib.axes._subplots.AxesSubplot at 0x7f9a3a14f080>

png

flights = sns.load_dataset('flights')
flights.head()
year month passengers
0 1949 January 112
1 1949 February 118
2 1949 March 132
3 1949 April 129
4 1949 May 121
tips.corr()
total_bill tip size
total_bill 1.000000 0.675734 0.598315
tip 0.675734 1.000000 0.489299
size 0.598315 0.489299 1.000000
sns.heatmap(tips.corr())
<matplotlib.axes._subplots.AxesSubplot at 0x7f9a3a0c4dd8>

png

pv_flights = flights.pivot_table(values='passengers', index='month', columns='year')
pv_flights
year 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960
month
January 112 115 145 171 196 204 242 284 315 340 360 417
February 118 126 150 180 196 188 233 277 301 318 342 391
March 132 141 178 193 236 235 267 317 356 362 406 419
April 129 135 163 181 235 227 269 313 348 348 396 461
May 121 125 172 183 229 234 270 318 355 363 420 472
June 135 149 178 218 243 264 315 374 422 435 472 535
July 148 170 199 230 264 302 364 413 465 491 548 622
August 148 170 199 242 272 293 347 405 467 505 559 606
September 136 158 184 209 237 259 312 355 404 404 463 508
October 119 133 162 191 211 229 274 306 347 359 407 461
November 104 114 146 172 180 203 237 271 305 310 362 390
December 118 140 166 194 201 229 278 306 336 337 405 432
sns.set_context('poster',font_scale=0.5)
sns.heatmap(flights.pivot_table(values='passengers', index='month', columns='year'), cmap='magma')
<matplotlib.axes._subplots.AxesSubplot at 0x7f9a39e5f550>

png

sns.clustermap(pv_flights) # rapprocher les mois et les années avec la corélation la plus forte
<seaborn.matrix.ClusterGrid at 0x7f9a3a135ef0>

png

sns.clustermap(pv_flights, cmap='coolwarm', standard_scale=1)
<seaborn.matrix.ClusterGrid at 0x7f9a396eff60>

png