Sei sulla pagina 1di 34

LIVE: Plotting using Seaborn.

ipynb - Colaboratory 05/05/20, 3:00 AM

Plotting using Seaborn


Agenda/Topics:
Modules/Tools come and go! Don't get stuck to tools.

How to code using the Seaborn reference + Google

https://seaborn.pydata.org/

https://seaborn.pydata.org/api.html

Go through some of the major plotting methods.

Focus: code walkthrough, code-composition, error messages

NOTE: Seaborn is built on top of MatPlotLib to simplify plotting

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")

# Load the example mpg dataset


mpg = sns.load_dataset("mpg")
print(type(mpg))

<class 'pandas.core.frame.DataFrame'>

# pretty print DataFrame


from tabulate import tabulate

#Ref: https://pypi.org/project/tabulate/
print(tabulate(mpg, headers='keys', tablefmt='github'))

| | mpg | cylinders | displacement | horsepower | weight | acce


|-----|-------|-------------|----------------|--------------|----------|-------
| 0 | 18 | 8 | 307 | 130 | 3504 |
| 1 | 15 | 8 | 350 | 165 | 3693 |
https://colab.research.google.com/drive/1bW7SEZk4FjsnEmIqnvQY77yfKVDlj5BR Page 1 of 34
LIVE: Plotting using Seaborn.ipynb - Colaboratory 05/05/20, 3:00 AM

| 1 | 15 | 8 | 350 | 165 | 3693 |


| 2 | 18 | 8 | 318 | 150 | 3436 |
| 3 | 16 | 8 | 304 | 150 | 3433 |
| 4 | 17 | 8 | 302 | 140 | 3449 |
| 5 | 15 | 8 | 429 | 198 | 4341 |
| 6 | 14 | 8 | 454 | 220 | 4354 |
| 7 | 14 | 8 | 440 | 215 | 4312 |
| 8 | 14 | 8 | 455 | 225 | 4425 |
| 9 | 15 | 8 | 390 | 190 | 3850 |
| 10 | 15 | 8 | 383 | 170 | 3563 |
| 11 | 14 | 8 | 340 | 160 | 3609 |
| 12 | 15 | 8 | 400 | 150 | 3761 |
| 13 | 14 | 8 | 455 | 225 | 3086 |
| 14 | 24 | 4 | 113 | 95 | 2372 |
| 15 | 22 | 6 | 198 | 95 | 2833 |
| 16 | 18 | 6 | 199 | 97 | 2774 |
| 17 | 21 | 6 | 200 | 85 | 2587 |
| 18 | 27 | 4 | 97 | 88 | 2130 |
| 19 | 26 | 4 | 97 | 46 | 1835 |
| 20 | 25 | 4 | 110 | 87 | 2672 |
| 21 | 24 | 4 | 107 | 90 | 2430 |
| 22 | 25 | 4 | 104 | 95 | 2375 |
| 23 | 26 | 4 | 121 | 113 | 2234 |
| 24 | 21 | 6 | 199 | 90 | 2648 |
| 25 | 10 | 8 | 360 | 215 | 4615 |
| 26 | 10 | 8 | 307 | 200 | 4376 |
| 27 | 11 | 8 | 318 | 210 | 4382 |
| 28 | 9 | 8 | 304 | 193 | 4732 |
| 29 | 27 | 4 | 97 | 88 | 2130 |
| 30 | 28 | 4 | 140 | 90 | 2264 |
| 31 | 25 | 4 | 113 | 95 | 2228 |
| 32 | 25 | 4 | 98 | nan | 2046 |
| 33 | 19 | 6 | 232 | 100 | 2634 |
| 34 | 16 | 6 | 225 | 105 | 3439 |
| 35 | 17 | 6 | 250 | 100 | 3329 |
| 36 | 19 | 6 | 250 | 88 | 3302 |
| 37 | 18 | 6 | 232 | 100 | 3288 |
| 38 | 14 | 8 | 350 | 165 | 4209 |
| 39 | 14 | 8 | 400 | 175 | 4464 |
| 40 | 14 | 8 | 351 | 153 | 4154 |
| 41 | 14 | 8 | 318 | 150 | 4096 |
| 42 | 12 | 8 | 383 | 180 | 4955 |
| 43 | 13 | 8 | 400 | 170 | 4746 |
| 44 | 13 | 8 | 400 | 175 | 5140 |
| 45 | 18 | 6 | 258 | 110 | 2962 |
| 46 | 22 | 4 | 140 | 72 | 2408 |
| 47 | 19 | 6 | 250 | 100 | 3282 |
| 48 | 18 | 6 | 250 | 88 | 3139 |
| 49 | 23 | 4 | 122 | 86 | 2220 |
| 50 | 28 | 4 | 116 | 90 | 2123 |
| 51 | 30 | 4 | 79 | 70 | 2074 |
| 52 | 30 | 4 | 88 | 76 | 2065 |
| 53 | 31 | 4 | 71 | 65 | 1773 |
| 54 | 35 | 4 | 72 | 69 | 1613 |
| 55 | 27 | 4 | 97 | 60 | 1834 |
https://colab.research.google.com/drive/1bW7SEZk4FjsnEmIqnvQY77yfKVDlj5BR Page 2 of 34
LIVE: Plotting using Seaborn.ipynb - Colaboratory 05/05/20, 3:00 AM

| 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

| 109 | 21 | 4 | 140 | 72 | 2401 |


| 110 | 22 | 4 | 108 | 94 | 2379 |
| 111 | 18 | 3 | 70 | 90 | 2124 |
| 112 | 19 | 4 | 122 | 85 | 2310 |
| 113 | 21 | 6 | 155 | 107 | 2472 |
| 114 | 26 | 4 | 98 | 90 | 2265 |
| 115 | 15 | 8 | 350 | 145 | 4082 |
| 116 | 16 | 8 | 400 | 230 | 4278 |
| 117 | 29 | 4 | 68 | 49 | 1867 |
| 118 | 24 | 4 | 116 | 75 | 2158 |
| 119 | 20 | 4 | 114 | 91 | 2582 |
| 120 | 19 | 4 | 121 | 112 | 2868 |
| 121 | 15 | 8 | 318 | 150 | 3399 |
| 122 | 24 | 4 | 121 | 110 | 2660 |
| 123 | 20 | 6 | 156 | 122 | 2807 |
| 124 | 11 | 8 | 350 | 180 | 3664 |
| 125 | 20 | 6 | 198 | 95 | 3102 |
| 126 | 21 | 6 | 200 | nan | 2875 |
| 127 | 19 | 6 | 232 | 100 | 2901 |
| 128 | 15 | 6 | 250 | 100 | 3336 |
| 129 | 31 | 4 | 79 | 67 | 1950 |
| 130 | 26 | 4 | 122 | 80 | 2451 |
| 131 | 32 | 4 | 71 | 65 | 1836 |
| 132 | 25 | 4 | 140 | 75 | 2542 |
| 133 | 16 | 6 | 250 | 100 | 3781 |
| 134 | 16 | 6 | 258 | 110 | 3632 |
| 135 | 18 | 6 | 225 | 105 | 3613 |
| 136 | 16 | 8 | 302 | 140 | 4141 |
| 137 | 13 | 8 | 350 | 150 | 4699 |
| 138 | 14 | 8 | 318 | 150 | 4457 |
| 139 | 14 | 8 | 302 | 140 | 4638 |
| 140 | 14 | 8 | 304 | 150 | 4257 |
| 141 | 29 | 4 | 98 | 83 | 2219 |
| 142 | 26 | 4 | 79 | 67 | 1963 |
| 143 | 26 | 4 | 97 | 78 | 2300 |
| 144 | 31 | 4 | 76 | 52 | 1649 |
| 145 | 32 | 4 | 83 | 61 | 2003 |
| 146 | 28 | 4 | 90 | 75 | 2125 |
| 147 | 24 | 4 | 90 | 75 | 2108 |
| 148 | 26 | 4 | 116 | 75 | 2246 |
| 149 | 24 | 4 | 120 | 97 | 2489 |
| 150 | 26 | 4 | 108 | 93 | 2391 |
| 151 | 31 | 4 | 79 | 67 | 2000 |
| 152 | 19 | 6 | 225 | 95 | 3264 |
| 153 | 18 | 6 | 250 | 105 | 3459 |
| 154 | 15 | 6 | 250 | 72 | 3432 |
| 155 | 15 | 6 | 250 | 72 | 3158 |
| 156 | 16 | 8 | 400 | 170 | 4668 |
| 157 | 15 | 8 | 350 | 145 | 4440 |
| 158 | 16 | 8 | 318 | 150 | 4498 |
| 159 | 14 | 8 | 351 | 148 | 4657 |
| 160 | 17 | 6 | 231 | 110 | 3907 |
| 161 | 16 | 6 | 250 | 105 | 3897 |
| 162 | 15 | 6 | 258 | 110 | 3730 |
| 163 | 18 | 6 | 225 | 95 | 3785 |
https://colab.research.google.com/drive/1bW7SEZk4FjsnEmIqnvQY77yfKVDlj5BR Page 4 of 34
LIVE: Plotting using Seaborn.ipynb - Colaboratory 05/05/20, 3:00 AM

| 163 | 18 | 6 | 225 | 95 | 3785 |


| 164 | 21 | 6 | 231 | 110 | 3039 |
| 165 | 20 | 8 | 262 | 110 | 3221 |
| 166 | 13 | 8 | 302 | 129 | 3169 |
| 167 | 29 | 4 | 97 | 75 | 2171 |
| 168 | 23 | 4 | 140 | 83 | 2639 |
| 169 | 20 | 6 | 232 | 100 | 2914 |
| 170 | 23 | 4 | 140 | 78 | 2592 |
| 171 | 24 | 4 | 134 | 96 | 2702 |
| 172 | 25 | 4 | 90 | 71 | 2223 |
| 173 | 24 | 4 | 119 | 97 | 2545 |
| 174 | 18 | 6 | 171 | 97 | 2984 |
| 175 | 29 | 4 | 90 | 70 | 1937 |
| 176 | 19 | 6 | 232 | 90 | 3211 |
| 177 | 23 | 4 | 115 | 95 | 2694 |
| 178 | 23 | 4 | 120 | 88 | 2957 |
| 179 | 22 | 4 | 121 | 98 | 2945 |
| 180 | 25 | 4 | 121 | 115 | 2671 |
| 181 | 33 | 4 | 91 | 53 | 1795 |
| 182 | 28 | 4 | 107 | 86 | 2464 |
| 183 | 25 | 4 | 116 | 81 | 2220 |
| 184 | 25 | 4 | 140 | 92 | 2572 |
| 185 | 26 | 4 | 98 | 79 | 2255 |
| 186 | 27 | 4 | 101 | 83 | 2202 |
| 187 | 17.5 | 8 | 305 | 140 | 4215 |
| 188 | 16 | 8 | 318 | 150 | 4190 |
| 189 | 15.5 | 8 | 304 | 120 | 3962 |
| 190 | 14.5 | 8 | 351 | 152 | 4215 |
| 191 | 22 | 6 | 225 | 100 | 3233 |
| 192 | 22 | 6 | 250 | 105 | 3353 |
| 193 | 24 | 6 | 200 | 81 | 3012 |
| 194 | 22.5 | 6 | 232 | 90 | 3085 |
| 195 | 29 | 4 | 85 | 52 | 2035 |
| 196 | 24.5 | 4 | 98 | 60 | 2164 |
| 197 | 29 | 4 | 90 | 70 | 1937 |
| 198 | 33 | 4 | 91 | 53 | 1795 |
| 199 | 20 | 6 | 225 | 100 | 3651 |
| 200 | 18 | 6 | 250 | 78 | 3574 |
| 201 | 18.5 | 6 | 250 | 110 | 3645 |
| 202 | 17.5 | 6 | 258 | 95 | 3193 |
| 203 | 29.5 | 4 | 97 | 71 | 1825 |
| 204 | 32 | 4 | 85 | 70 | 1990 |
| 205 | 28 | 4 | 97 | 75 | 2155 |
| 206 | 26.5 | 4 | 140 | 72 | 2565 |
| 207 | 20 | 4 | 130 | 102 | 3150 |
| 208 | 13 | 8 | 318 | 150 | 3940 |
| 209 | 19 | 4 | 120 | 88 | 3270 |
| 210 | 19 | 6 | 156 | 108 | 2930 |
| 211 | 16.5 | 6 | 168 | 120 | 3820 |
| 212 | 16.5 | 8 | 350 | 180 | 4380 |
| 213 | 13 | 8 | 350 | 145 | 4055 |
| 214 | 13 | 8 | 302 | 130 | 3870 |
| 215 | 13 | 8 | 318 | 150 | 3755 |
| 216 | 31.5 | 4 | 98 | 68 | 2045 |
| 217 | 30 | 4 | 111 | 80 | 2155 |
https://colab.research.google.com/drive/1bW7SEZk4FjsnEmIqnvQY77yfKVDlj5BR Page 5 of 34
LIVE: Plotting using Seaborn.ipynb - Colaboratory 05/05/20, 3:00 AM

| 217 | 30 | 4 | 111 | 80 | 2155 |


| 218 | 36 | 4 | 79 | 58 | 1825 |
| 219 | 25.5 | 4 | 122 | 96 | 2300 |
| 220 | 33.5 | 4 | 85 | 70 | 1945 |
| 221 | 17.5 | 8 | 305 | 145 | 3880 |
| 222 | 17 | 8 | 260 | 110 | 4060 |
| 223 | 15.5 | 8 | 318 | 145 | 4140 |
| 224 | 15 | 8 | 302 | 130 | 4295 |
| 225 | 17.5 | 6 | 250 | 110 | 3520 |
| 226 | 20.5 | 6 | 231 | 105 | 3425 |
| 227 | 19 | 6 | 225 | 100 | 3630 |
| 228 | 18.5 | 6 | 250 | 98 | 3525 |
| 229 | 16 | 8 | 400 | 180 | 4220 |
| 230 | 15.5 | 8 | 350 | 170 | 4165 |
| 231 | 15.5 | 8 | 400 | 190 | 4325 |
| 232 | 16 | 8 | 351 | 149 | 4335 |
| 233 | 29 | 4 | 97 | 78 | 1940 |
| 234 | 24.5 | 4 | 151 | 88 | 2740 |
| 235 | 26 | 4 | 97 | 75 | 2265 |
| 236 | 25.5 | 4 | 140 | 89 | 2755 |
| 237 | 30.5 | 4 | 98 | 63 | 2051 |
| 238 | 33.5 | 4 | 98 | 83 | 2075 |
| 239 | 30 | 4 | 97 | 67 | 1985 |
| 240 | 30.5 | 4 | 97 | 78 | 2190 |
| 241 | 22 | 6 | 146 | 97 | 2815 |
| 242 | 21.5 | 4 | 121 | 110 | 2600 |
| 243 | 21.5 | 3 | 80 | 110 | 2720 |
| 244 | 43.1 | 4 | 90 | 48 | 1985 |
| 245 | 36.1 | 4 | 98 | 66 | 1800 |
| 246 | 32.8 | 4 | 78 | 52 | 1985 |
| 247 | 39.4 | 4 | 85 | 70 | 2070 |
| 248 | 36.1 | 4 | 91 | 60 | 1800 |
| 249 | 19.9 | 8 | 260 | 110 | 3365 |
| 250 | 19.4 | 8 | 318 | 140 | 3735 |
| 251 | 20.2 | 8 | 302 | 139 | 3570 |
| 252 | 19.2 | 6 | 231 | 105 | 3535 |
| 253 | 20.5 | 6 | 200 | 95 | 3155 |
| 254 | 20.2 | 6 | 200 | 85 | 2965 |
| 255 | 25.1 | 4 | 140 | 88 | 2720 |
| 256 | 20.5 | 6 | 225 | 100 | 3430 |
| 257 | 19.4 | 6 | 232 | 90 | 3210 |
| 258 | 20.6 | 6 | 231 | 105 | 3380 |
| 259 | 20.8 | 6 | 200 | 85 | 3070 |
| 260 | 18.6 | 6 | 225 | 110 | 3620 |
| 261 | 18.1 | 6 | 258 | 120 | 3410 |
| 262 | 19.2 | 8 | 305 | 145 | 3425 |
| 263 | 17.7 | 6 | 231 | 165 | 3445 |
| 264 | 18.1 | 8 | 302 | 139 | 3205 |
| 265 | 17.5 | 8 | 318 | 140 | 4080 |
| 266 | 30 | 4 | 98 | 68 | 2155 |
| 267 | 27.5 | 4 | 134 | 95 | 2560 |
| 268 | 27.2 | 4 | 119 | 97 | 2300 |
| 269 | 30.9 | 4 | 105 | 75 | 2230 |
| 270 | 21.1 | 4 | 134 | 95 | 2515 |
| 271 | 23.2 | 4 | 156 | 105 | 2745 |
https://colab.research.google.com/drive/1bW7SEZk4FjsnEmIqnvQY77yfKVDlj5BR Page 6 of 34
LIVE: Plotting using Seaborn.ipynb - Colaboratory 05/05/20, 3:00 AM

| 271 | 23.2 | 4 | 156 | 105 | 2745 |


| 272 | 23.8 | 4 | 151 | 85 | 2855 |
| 273 | 23.9 | 4 | 119 | 97 | 2405 |
| 274 | 20.3 | 5 | 131 | 103 | 2830 |
| 275 | 17 | 6 | 163 | 125 | 3140 |
| 276 | 21.6 | 4 | 121 | 115 | 2795 |
| 277 | 16.2 | 6 | 163 | 133 | 3410 |
| 278 | 31.5 | 4 | 89 | 71 | 1990 |
| 279 | 29.5 | 4 | 98 | 68 | 2135 |
| 280 | 21.5 | 6 | 231 | 115 | 3245 |
| 281 | 19.8 | 6 | 200 | 85 | 2990 |
| 282 | 22.3 | 4 | 140 | 88 | 2890 |
| 283 | 20.2 | 6 | 232 | 90 | 3265 |
| 284 | 20.6 | 6 | 225 | 110 | 3360 |
| 285 | 17 | 8 | 305 | 130 | 3840 |
| 286 | 17.6 | 8 | 302 | 129 | 3725 |
| 287 | 16.5 | 8 | 351 | 138 | 3955 |
| 288 | 18.2 | 8 | 318 | 135 | 3830 |
| 289 | 16.9 | 8 | 350 | 155 | 4360 |
| 290 | 15.5 | 8 | 351 | 142 | 4054 |
| 291 | 19.2 | 8 | 267 | 125 | 3605 |
| 292 | 18.5 | 8 | 360 | 150 | 3940 |
| 293 | 31.9 | 4 | 89 | 71 | 1925 |
| 294 | 34.1 | 4 | 86 | 65 | 1975 |
| 295 | 35.7 | 4 | 98 | 80 | 1915 |
| 296 | 27.4 | 4 | 121 | 80 | 2670 |
| 297 | 25.4 | 5 | 183 | 77 | 3530 |
| 298 | 23 | 8 | 350 | 125 | 3900 |
| 299 | 27.2 | 4 | 141 | 71 | 3190 |
| 300 | 23.9 | 8 | 260 | 90 | 3420 |
| 301 | 34.2 | 4 | 105 | 70 | 2200 |
| 302 | 34.5 | 4 | 105 | 70 | 2150 |
| 303 | 31.8 | 4 | 85 | 65 | 2020 |
| 304 | 37.3 | 4 | 91 | 69 | 2130 |
| 305 | 28.4 | 4 | 151 | 90 | 2670 |
| 306 | 28.8 | 6 | 173 | 115 | 2595 |
| 307 | 26.8 | 6 | 173 | 115 | 2700 |
| 308 | 33.5 | 4 | 151 | 90 | 2556 |
| 309 | 41.5 | 4 | 98 | 76 | 2144 |
| 310 | 38.1 | 4 | 89 | 60 | 1968 |
| 311 | 32.1 | 4 | 98 | 70 | 2120 |
| 312 | 37.2 | 4 | 86 | 65 | 2019 |
| 313 | 28 | 4 | 151 | 90 | 2678 |
| 314 | 26.4 | 4 | 140 | 88 | 2870 |
| 315 | 24.3 | 4 | 151 | 90 | 3003 |
| 316 | 19.1 | 6 | 225 | 90 | 3381 |
| 317 | 34.3 | 4 | 97 | 78 | 2188 |
| 318 | 29.8 | 4 | 134 | 90 | 2711 |
| 319 | 31.3 | 4 | 120 | 75 | 2542 |
| 320 | 37 | 4 | 119 | 92 | 2434 |
| 321 | 32.2 | 4 | 108 | 75 | 2265 |
| 322 | 46.6 | 4 | 86 | 65 | 2110 |
| 323 | 27.9 | 4 | 156 | 105 | 2800 |
| 324 | 40.8 | 4 | 85 | 65 | 2110 |
| 325 | 44.3 | 4 | 90 | 48 | 2085 |
https://colab.research.google.com/drive/1bW7SEZk4FjsnEmIqnvQY77yfKVDlj5BR Page 7 of 34
LIVE: Plotting using Seaborn.ipynb - Colaboratory 05/05/20, 3:00 AM

| 325 | 44.3 | 4 | 90 | 48 | 2085 |


| 326 | 43.4 | 4 | 90 | 48 | 2335 |
| 327 | 36.4 | 5 | 121 | 67 | 2950 |
| 328 | 30 | 4 | 146 | 67 | 3250 |
| 329 | 44.6 | 4 | 91 | 67 | 1850 |
| 330 | 40.9 | 4 | 85 | nan | 1835 |
| 331 | 33.8 | 4 | 97 | 67 | 2145 |
| 332 | 29.8 | 4 | 89 | 62 | 1845 |
| 333 | 32.7 | 6 | 168 | 132 | 2910 |
| 334 | 23.7 | 3 | 70 | 100 | 2420 |
| 335 | 35 | 4 | 122 | 88 | 2500 |
| 336 | 23.6 | 4 | 140 | nan | 2905 |
| 337 | 32.4 | 4 | 107 | 72 | 2290 |
| 338 | 27.2 | 4 | 135 | 84 | 2490 |
| 339 | 26.6 | 4 | 151 | 84 | 2635 |
| 340 | 25.8 | 4 | 156 | 92 | 2620 |
| 341 | 23.5 | 6 | 173 | 110 | 2725 |
| 342 | 30 | 4 | 135 | 84 | 2385 |
| 343 | 39.1 | 4 | 79 | 58 | 1755 |
| 344 | 39 | 4 | 86 | 64 | 1875 |
| 345 | 35.1 | 4 | 81 | 60 | 1760 |
| 346 | 32.3 | 4 | 97 | 67 | 2065 |
| 347 | 37 | 4 | 85 | 65 | 1975 |
| 348 | 37.7 | 4 | 89 | 62 | 2050 |
| 349 | 34.1 | 4 | 91 | 68 | 1985 |
| 350 | 34.7 | 4 | 105 | 63 | 2215 |
| 351 | 34.4 | 4 | 98 | 65 | 2045 |
| 352 | 29.9 | 4 | 98 | 65 | 2380 |
| 353 | 33 | 4 | 105 | 74 | 2190 |
| 354 | 34.5 | 4 | 100 | nan | 2320 |
| 355 | 33.7 | 4 | 107 | 75 | 2210 |
| 356 | 32.4 | 4 | 108 | 75 | 2350 |
| 357 | 32.9 | 4 | 119 | 100 | 2615 |
| 358 | 31.6 | 4 | 120 | 74 | 2635 |
| 359 | 28.1 | 4 | 141 | 80 | 3230 |
| 360 | 30.7 | 6 | 145 | 76 | 3160 |
| 361 | 25.4 | 6 | 168 | 116 | 2900 |
| 362 | 24.2 | 6 | 146 | 120 | 2930 |
| 363 | 22.4 | 6 | 231 | 110 | 3415 |
| 364 | 26.6 | 8 | 350 | 105 | 3725 |
| 365 | 20.2 | 6 | 200 | 88 | 3060 |
| 366 | 17.6 | 6 | 225 | 85 | 3465 |
| 367 | 28 | 4 | 112 | 88 | 2605 |
| 368 | 27 | 4 | 112 | 88 | 2640 |
| 369 | 34 | 4 | 112 | 88 | 2395 |
| 370 | 31 | 4 | 112 | 85 | 2575 |
| 371 | 29 | 4 | 135 | 84 | 2525 |
| 372 | 27 | 4 | 151 | 90 | 2735 |
| 373 | 24 | 4 | 140 | 92 | 2865 |
| 374 | 23 | 4 | 151 | nan | 3035 |
| 375 | 36 | 4 | 105 | 74 | 1980 |
| 376 | 37 | 4 | 91 | 68 | 2025 |
| 377 | 31 | 4 | 91 | 68 | 1970 |
| 378 | 38 | 4 | 105 | 63 | 2125 |
| 379 | 36 | 4 | 98 | 70 | 2125 |
https://colab.research.google.com/drive/1bW7SEZk4FjsnEmIqnvQY77yfKVDlj5BR Page 8 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 |

# Plot miles per gallon against horsepower

# 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

# GRID is very important for readability of a plot


sns.set(style="whitegrid")
sns.scatterplot(x="horsepower", y="mpg", data=mpg)

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

# different colored points for different countries/origin


sns.set(style="whitegrid")
sns.scatterplot(x="horsepower", y="mpg", data=mpg, hue='origin').set_title('SP'

# Conclusion = ?

Text(0.5, 1.0, 'SP')

https://colab.research.google.com/drive/1bW7SEZk4FjsnEmIqnvQY77yfKVDlj5BR Page 10 of 34
LIVE: Plotting using Seaborn.ipynb - Colaboratory 05/05/20, 3:00 AM

# size of each point proportional to the weight of car.


sns.set(style="whitegrid")
sns.scatterplot(x="horsepower", y="mpg", data=mpg, hue='origin', size="weight")

# Conclusion = ?

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

# custom-sizes for each bubble


# size of each point proportional to the weight of car.
sns.set(style="whitegrid")
sns.scatterplot(x="horsepower", y="mpg", data=mpg, hue='origin', \
size="weight", sizes = (40,400))

<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

import matplotlib as plt

sns.set(style="whitegrid")
plt.style.use("dark_background")

sns.scatterplot(x="horsepower", y="mpg", data=mpg, hue='origin', \


size="weight", sizes = (20,300), palette = 'muted')

<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))

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
5 25.29 4.71 Male No Sun Dinner 4
6 8.77 2.00 Male No Sun Dinner 2
7 26.88 3.12 Male No Sun Dinner 4
8 15.04 1.96 Male No Sun Dinner 2
9 14.78 3.23 Male No Sun Dinner 2

https://colab.research.google.com/drive/1bW7SEZk4FjsnEmIqnvQY77yfKVDlj5BR Page 14 of 34
LIVE: Plotting using Seaborn.ipynb - Colaboratory 05/05/20, 3:00 AM

# Plot the data for


sns.set(style="ticks", color_codes=True)
g = sns.FacetGrid(tips, col="time", row="smoker") #time and smoker are categorical
print(type(g))

<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")

NameError: name 'plt' is not defined

SEARCH STACK OVERFLOW

https://colab.research.google.com/drive/1bW7SEZk4FjsnEmIqnvQY77yfKVDlj5BR Page 15 of 34
LIVE: Plotting using Seaborn.ipynb - Colaboratory 05/05/20, 3:00 AM

import matplotlib.pyplot as plt


g.map(plt.hist, "total_bill")

---------------------------------------------------------------------------
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

ValueError: Axes instance argument was not found in a figure

SEARCH STACK OVERFLOW

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

bins = np.arange(0, 65, 3)


g = sns.FacetGrid(tips, col="time", row="smoker")
g.map(plt.hist, "total_bill", bins=bins)

#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

g = sns.FacetGrid(tips, col="time", row="smoker")


g = g.map(plt.scatter, "total_bill", "tip", edgecolor="w" )

g = sns.FacetGrid(tips, col="time", hue="smoker")


#print(type(g.map(plt.scatter, "total_bill", "tip", edgecolor="w")))
g = (g.map(plt.scatter, "total_bill", "tip", edgecolor="w")).add_legend()

import numpy as np

bins = np.arange(0, 65, 3)

g = sns.FacetGrid(tips, row="day", col = "smoker" , height=4)

https://colab.research.google.com/drive/1bW7SEZk4FjsnEmIqnvQY77yfKVDlj5BR Page 18 of 34
LIVE: Plotting using Seaborn.ipynb - Colaboratory 05/05/20, 3:00 AM

g = g.map(plt.hist, "total_bill", bins=bins)

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

g = sns.FacetGrid(att, col="subject", col_wrap=5,height=1.5)


g = g.map(plt.plot, "solutions", "score", marker=".")

LinePlot
https://seaborn.pydata.org/generated/seaborn.lineplot.html#seaborn.lineplot

import seaborn as sns


from tabulate import tabulate

sns.set(style="darkgrid")

# Load an example dataset with long-form data


fmri = sns.load_dataset("fmri")
print(tabulate(fmri, headers='keys', tablefmt='github'))

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")

sns.lineplot(x="timepoint", y="signal", data=fmri)

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

sns.lineplot(x="timepoint", y="signal", hue="event", data=fmri)

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:

import seaborn as sns, numpy as np


sns.set();
np.random.seed(10)
x = np.random.randn(100)
sns.distplot(x)

https://colab.research.google.com/drive/1bW7SEZk4FjsnEmIqnvQY77yfKVDlj5BR Page 27 of 34
LIVE: Plotting using Seaborn.ipynb - Colaboratory 05/05/20, 3:00 AM

import numpy as np;


import seaborn as sns;
np.random.seed(10)

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

g = sns.jointplot(x="total_bill", y="tip", data=tips)

https://colab.research.google.com/drive/1bW7SEZk4FjsnEmIqnvQY77yfKVDlj5BR Page 31 of 34
LIVE: Plotting using Seaborn.ipynb - Colaboratory 05/05/20, 3:00 AM

g = sns.jointplot(x="total_bill", y="tip", data=tips, kind="kde", color='g')

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

g = (sns.jointplot(x, y, kind="kde").set_axis_labels("x", "y"))

https://colab.research.google.com/drive/1bW7SEZk4FjsnEmIqnvQY77yfKVDlj5BR Page 34 of 34

Potrebbero piacerti anche