Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
https://seaborn.pydata.org/
https://seaborn.pydata.org/api.html
Pre-requisites:
Plotting for Exploratory Data Anlaysis.
Python Programming: course videos + live sessions
Scatterplot
# source: https://seaborn.pydata.org/examples/scatter_bubbles.html
import seaborn as sns
sns.set(style="white")
<class 'pandas.core.frame.DataFrame'>
#Ref: https://pypi.org/project/tabulate/
print(tabulate(mpg, headers='keys', tablefmt='github'))
| 55 | 27 | 4 | 97 | 60 | 1834 |
| 56 | 26 | 4 | 91 | 70 | 1955 |
| 57 | 24 | 4 | 113 | 95 | 2278 |
| 58 | 25 | 4 | 97.5 | 80 | 2126 |
| 59 | 23 | 4 | 97 | 54 | 2254 |
| 60 | 20 | 4 | 140 | 90 | 2408 |
| 61 | 21 | 4 | 122 | 86 | 2226 |
| 62 | 13 | 8 | 350 | 165 | 4274 |
| 63 | 14 | 8 | 400 | 175 | 4385 |
| 64 | 15 | 8 | 318 | 150 | 4135 |
| 65 | 14 | 8 | 351 | 153 | 4129 |
| 66 | 17 | 8 | 304 | 150 | 3672 |
| 67 | 11 | 8 | 429 | 208 | 4633 |
| 68 | 13 | 8 | 350 | 155 | 4502 |
| 69 | 12 | 8 | 350 | 160 | 4456 |
| 70 | 13 | 8 | 400 | 190 | 4422 |
| 71 | 19 | 3 | 70 | 97 | 2330 |
| 72 | 15 | 8 | 304 | 150 | 3892 |
| 73 | 13 | 8 | 307 | 130 | 4098 |
| 74 | 13 | 8 | 302 | 140 | 4294 |
| 75 | 14 | 8 | 318 | 150 | 4077 |
| 76 | 18 | 4 | 121 | 112 | 2933 |
| 77 | 22 | 4 | 121 | 76 | 2511 |
| 78 | 21 | 4 | 120 | 87 | 2979 |
| 79 | 26 | 4 | 96 | 69 | 2189 |
| 80 | 22 | 4 | 122 | 86 | 2395 |
| 81 | 28 | 4 | 97 | 92 | 2288 |
| 82 | 23 | 4 | 120 | 97 | 2506 |
| 83 | 28 | 4 | 98 | 80 | 2164 |
| 84 | 27 | 4 | 97 | 88 | 2100 |
| 85 | 13 | 8 | 350 | 175 | 4100 |
| 86 | 14 | 8 | 304 | 150 | 3672 |
| 87 | 13 | 8 | 350 | 145 | 3988 |
| 88 | 14 | 8 | 302 | 137 | 4042 |
| 89 | 15 | 8 | 318 | 150 | 3777 |
| 90 | 12 | 8 | 429 | 198 | 4952 |
| 91 | 13 | 8 | 400 | 150 | 4464 |
| 92 | 13 | 8 | 351 | 158 | 4363 |
| 93 | 14 | 8 | 318 | 150 | 4237 |
| 94 | 13 | 8 | 440 | 215 | 4735 |
| 95 | 12 | 8 | 455 | 225 | 4951 |
| 96 | 13 | 8 | 360 | 175 | 3821 |
| 97 | 18 | 6 | 225 | 105 | 3121 |
| 98 | 16 | 6 | 250 | 100 | 3278 |
| 99 | 18 | 6 | 232 | 100 | 2945 |
| 100 | 18 | 6 | 250 | 88 | 3021 |
| 101 | 23 | 6 | 198 | 95 | 2904 |
| 102 | 26 | 4 | 97 | 46 | 1950 |
| 103 | 11 | 8 | 400 | 150 | 4997 |
| 104 | 12 | 8 | 400 | 167 | 4906 |
| 105 | 13 | 8 | 360 | 170 | 4654 |
| 106 | 12 | 8 | 350 | 180 | 4499 |
| 107 | 18 | 6 | 232 | 100 | 2789 |
| 108 | 20 | 4 | 97 | 88 | 2279 |
| 109 | 21 | 4 | 140 | 72 | 2401 |
https://colab.research.google.com/drive/1bW7SEZk4FjsnEmIqnvQY77yfKVDlj5BR Page 3 of 34
LIVE: Plotting using Seaborn.ipynb - Colaboratory 05/05/20, 3:00 AM
| 379 | 36 | 4 | 98 | 70 | 2125 |
| 380 | 36 | 4 | 120 | 88 | 2160 |
| 381 | 36 | 4 | 107 | 75 | 2205 |
| 382 | 34 | 4 | 108 | 70 | 2245 |
| 383 | 38 | 4 | 91 | 67 | 1965 |
| 384 | 32 | 4 | 91 | 67 | 1965 |
| 385 | 38 | 4 | 91 | 67 | 1995 |
| 386 | 25 | 6 | 181 | 110 | 2945 |
| 387 | 38 | 6 | 262 | 85 | 3015 |
| 388 | 26 | 4 | 156 | 92 | 2585 |
| 389 | 22 | 6 | 232 | 112 | 2835 |
| 390 | 32 | 4 | 144 | 96 | 2665 |
| 391 | 36 | 4 | 135 | 84 | 2370 |
| 392 | 27 | 4 | 151 | 90 | 2950 |
| 393 | 27 | 4 | 140 | 86 | 2790 |
| 394 | 44 | 4 | 97 | 52 | 2130 |
| 395 | 32 | 4 | 135 | 84 | 2295 |
| 396 | 28 | 4 | 120 | 79 | 2625 |
| 397 | 31 | 4 | 119 | 82 | 2720 |
# Ref: https://seaborn.pydata.org/generated/seaborn.scatterplot.html
sns.scatterplot(x="horsepower", y="mpg", data=mpg)
# Conclusion = ?
<matplotlib.axes._subplots.AxesSubplot at 0x7f5c91e3a7b8>
https://colab.research.google.com/drive/1bW7SEZk4FjsnEmIqnvQY77yfKVDlj5BR Page 9 of 34
LIVE: Plotting using Seaborn.ipynb - Colaboratory 05/05/20, 3:00 AM
<matplotlib.axes._subplots.AxesSubplot at 0x7f5c8ec49780>
# Conclusion = ?
https://colab.research.google.com/drive/1bW7SEZk4FjsnEmIqnvQY77yfKVDlj5BR Page 10 of 34
LIVE: Plotting using Seaborn.ipynb - Colaboratory 05/05/20, 3:00 AM
# Conclusion = ?
<matplotlib.axes._subplots.AxesSubplot at 0x7f2f95d9c710>
<matplotlib.axes._subplots.AxesSubplot at 0x7f2f95cf3b70>
https://colab.research.google.com/drive/1bW7SEZk4FjsnEmIqnvQY77yfKVDlj5BR Page 11 of 34
LIVE: Plotting using Seaborn.ipynb - Colaboratory 05/05/20, 3:00 AM
# Color-Pallette
#Ref: https://seaborn.pydata.org/generated/seaborn.color_palette.html#seaborn.color
sns.set(style="whitegrid")
sns.scatterplot(x="horsepower", y="mpg", data=mpg, hue='origin', \
size="weight", sizes = (40,400), palette = 'muted')
<matplotlib.axes._subplots.AxesSubplot at 0x7f2f9421c3c8>
https://colab.research.google.com/drive/1bW7SEZk4FjsnEmIqnvQY77yfKVDlj5BR Page 12 of 34
LIVE: Plotting using Seaborn.ipynb - Colaboratory 05/05/20, 3:00 AM
# dark-plot
# Google: " Searborn plot with dark background"
# https://gist.github.com/mwaskom/7be0963cc57f6c89f7b2
sns.set(style="whitegrid")
plt.style.use("dark_background")
<matplotlib.axes._subplots.AxesSubplot at 0x7f2f9420c6d8>
FacetGrid
Multi-plot grid for plotting conditional relationships.
https://seaborn.pydata.org/generated/seaborn.FacetGrid.html
https://colab.research.google.com/drive/1bW7SEZk4FjsnEmIqnvQY77yfKVDlj5BR Page 13 of 34
LIVE: Plotting using Seaborn.ipynb - Colaboratory 05/05/20, 3:00 AM
#load data
from tabulate import tabulate
import seaborn as sns
tips = sns.load_dataset("tips")
#Ref: https://pypi.org/project/tabulate/
#print(tabulate(tips, headers='keys', tablefmt='github'))
print(tips.head(10))
https://colab.research.google.com/drive/1bW7SEZk4FjsnEmIqnvQY77yfKVDlj5BR Page 14 of 34
LIVE: Plotting using Seaborn.ipynb - Colaboratory 05/05/20, 3:00 AM
<class 'seaborn.axisgrid.FacetGrid'>
# Ref: https://seaborn.pydata.org/generated/seaborn.FacetGrid.map.html#seaborn.Face
g.map(plt.hist, "total_bill")
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-14-92103c846012> in <module>()
----> 1 g.map(plt.hist, "total_bill")
https://colab.research.google.com/drive/1bW7SEZk4FjsnEmIqnvQY77yfKVDlj5BR Page 15 of 34
LIVE: Plotting using Seaborn.ipynb - Colaboratory 05/05/20, 3:00 AM
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-15-933fc3a7bf14> in <module>()
1 import matplotlib.pyplot as plt
----> 2 g.map(plt.hist, "total_bill")
2 frames
/usr/local/lib/python3.6/dist-packages/matplotlib/pyplot.py in sca(ax)
856 m.canvas.figure.sca(ax)
857 return
--> 858 raise ValueError("Axes instance argument was not found in a figure"
859
860
https://colab.research.google.com/drive/1bW7SEZk4FjsnEmIqnvQY77yfKVDlj5BR Page 16 of 34
LIVE: Plotting using Seaborn.ipynb - Colaboratory 05/05/20, 3:00 AM
import numpy as np
#Conclusion?
<seaborn.axisgrid.FacetGrid at 0x7f0c5e157668>
https://colab.research.google.com/drive/1bW7SEZk4FjsnEmIqnvQY77yfKVDlj5BR Page 17 of 34
LIVE: Plotting using Seaborn.ipynb - Colaboratory 05/05/20, 3:00 AM
import numpy as np
https://colab.research.google.com/drive/1bW7SEZk4FjsnEmIqnvQY77yfKVDlj5BR Page 18 of 34
LIVE: Plotting using Seaborn.ipynb - Colaboratory 05/05/20, 3:00 AM
https://colab.research.google.com/drive/1bW7SEZk4FjsnEmIqnvQY77yfKVDlj5BR Page 19 of 34
LIVE: Plotting using Seaborn.ipynb - Colaboratory 05/05/20, 3:00 AM
att = sns.load_dataset("attention")
print(tabulate(att, headers='keys', tablefmt='github'))
https://colab.research.google.com/drive/1bW7SEZk4FjsnEmIqnvQY77yfKVDlj5BR Page 20 of 34
LIVE: Plotting using Seaborn.ipynb - Colaboratory 05/05/20, 3:00 AM
https://colab.research.google.com/drive/1bW7SEZk4FjsnEmIqnvQY77yfKVDlj5BR Page 21 of 34
LIVE: Plotting using Seaborn.ipynb - Colaboratory 05/05/20, 3:00 AM
LinePlot
https://seaborn.pydata.org/generated/seaborn.lineplot.html#seaborn.lineplot
sns.set(style="darkgrid")
https://colab.research.google.com/drive/1bW7SEZk4FjsnEmIqnvQY77yfKVDlj5BR Page 22 of 34
LIVE: Plotting using Seaborn.ipynb - Colaboratory 05/05/20, 3:00 AM
https://colab.research.google.com/drive/1bW7SEZk4FjsnEmIqnvQY77yfKVDlj5BR Page 23 of 34
LIVE: Plotting using Seaborn.ipynb - Colaboratory 05/05/20, 3:00 AM
sns.lineplot(x="timepoint", y="signal")
np.mean(fmri[fmri["timepoint"] == 5]["signal"])
https://colab.research.google.com/drive/1bW7SEZk4FjsnEmIqnvQY77yfKVDlj5BR Page 24 of 34
LIVE: Plotting using Seaborn.ipynb - Colaboratory 05/05/20, 3:00 AM
ax = sns.lineplot(x="timepoint", y="signal",
hue="region", style="event", data=fmri)
import numpy as np
import pandas as pd
import seaborn as sns
sns.set(style="whitegrid")
rs = np.random.RandomState(365)
values = rs.randn(365, 2)
dates = pd.date_range("1 1 2016", periods=365, freq="D")
data = pd.DataFrame(values, dates, columns=["A", "B"])
https://colab.research.google.com/drive/1bW7SEZk4FjsnEmIqnvQY77yfKVDlj5BR Page 25 of 34
LIVE: Plotting using Seaborn.ipynb - Colaboratory 05/05/20, 3:00 AM
print(tabulate(data, headers='keys',
sns.lineplot(data=data, tablefmt='github'))
palette="tab10", linewidth=2.5)
https://colab.research.google.com/drive/1bW7SEZk4FjsnEmIqnvQY77yfKVDlj5BR Page 26 of 34
LIVE: Plotting using Seaborn.ipynb - Colaboratory 05/05/20, 3:00 AM
Distribution plots
-
distplot: https://seaborn.pydata.org/generated/seaborn.distplot.html#seaborn.distplot
kdeplot:
jointplot:
https://colab.research.google.com/drive/1bW7SEZk4FjsnEmIqnvQY77yfKVDlj5BR Page 27 of 34
LIVE: Plotting using Seaborn.ipynb - Colaboratory 05/05/20, 3:00 AM
x = np.random.randn(100)
ax = sns.kdeplot(x)
ax = sns.kdeplot(x, bw=.1)
https://colab.research.google.com/drive/1bW7SEZk4FjsnEmIqnvQY77yfKVDlj5BR Page 28 of 34
LIVE: Plotting using Seaborn.ipynb - Colaboratory 05/05/20, 3:00 AM
ax = sns.kdeplot(x, bw=.3)
ax = sns.kdeplot(x, bw=1)
#JointPlot
#https://seaborn.pydata.org/generated/seaborn.jointplot.html#seaborn.jointplot
import numpy as np, pandas as pd; np.random.seed(0)
import seaborn as sns; sns.set(style="white", color_codes=True)
tips = sns.load_dataset("tips")
print(tabulate(tips, headers='keys', tablefmt='github'))
https://colab.research.google.com/drive/1bW7SEZk4FjsnEmIqnvQY77yfKVDlj5BR Page 29 of 34
LIVE: Plotting using Seaborn.ipynb - Colaboratory 05/05/20, 3:00 AM
https://colab.research.google.com/drive/1bW7SEZk4FjsnEmIqnvQY77yfKVDlj5BR Page 30 of 34
LIVE: Plotting using Seaborn.ipynb - Colaboratory 05/05/20, 3:00 AM
https://colab.research.google.com/drive/1bW7SEZk4FjsnEmIqnvQY77yfKVDlj5BR Page 31 of 34
LIVE: Plotting using Seaborn.ipynb - Colaboratory 05/05/20, 3:00 AM
https://colab.research.google.com/drive/1bW7SEZk4FjsnEmIqnvQY77yfKVDlj5BR Page 32 of 34
LIVE: Plotting using Seaborn.ipynb - Colaboratory 05/05/20, 3:00 AM
x, y = np.random.randn(2, 3000)
print(type(sns.jointplot(x, y, kind="kde")))
https://colab.research.google.com/drive/1bW7SEZk4FjsnEmIqnvQY77yfKVDlj5BR Page 33 of 34
LIVE: Plotting using Seaborn.ipynb - Colaboratory 05/05/20, 3:00 AM
https://colab.research.google.com/drive/1bW7SEZk4FjsnEmIqnvQY77yfKVDlj5BR Page 34 of 34