SEABORN - SWARM PLOT Padhai ClassWork
Padhi_SwarmPlots

Swarm Plots, Strip Plots, Cat Plots

In [ ]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
sns.set(color_codes = True)
In [ ]:
df = sns.load_dataset('diamonds')
In [ ]:
df.head()
Out[ ]:
carat cut color clarity depth table price x y z
0 0.23 Ideal E SI2 61.5 55.0 326 3.95 3.98 2.43
1 0.21 Premium E SI1 59.8 61.0 326 3.89 3.84 2.31
2 0.23 Good E VS1 56.9 65.0 327 4.05 4.07 2.31
3 0.29 Premium I VS2 62.4 58.0 334 4.20 4.23 2.63
4 0.31 Good J SI2 63.3 58.0 335 4.34 4.35 2.75
In [ ]:
sns.swarmplot(x='caret')
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-4-00f5b161ca42> in <module>()
----> 1 sns.swarmplot(x='caret')

/usr/local/lib/python3.7/dist-packages/seaborn/_decorators.py in inner_f(*args, **kwargs)
     44             )
     45         kwargs.update({k: arg for k, arg in zip(sig.parameters, args)})
---> 46         return f(**kwargs)
     47     return inner_f
     48 

/usr/local/lib/python3.7/dist-packages/seaborn/categorical.py in swarmplot(x, y, hue, data, order, hue_order, dodge, orient, color, palette, size, edgecolor, linewidth, ax, **kwargs)
   3003 
   3004     plotter = _SwarmPlotter(x, y, hue, data, order, hue_order,
-> 3005                             dodge, orient, color, palette)
   3006     if ax is None:
   3007         ax = plt.gca()

/usr/local/lib/python3.7/dist-packages/seaborn/categorical.py in __init__(self, x, y, hue, data, order, hue_order, dodge, orient, color, palette)
   1170                  dodge, orient, color, palette):
   1171         """Initialize the plotter."""
-> 1172         self.establish_variables(x, y, hue, data, orient, order, hue_order)
   1173         self.establish_colors(color, palette, 1)
   1174 

/usr/local/lib/python3.7/dist-packages/seaborn/categorical.py in establish_variables(self, x, y, hue, data, orient, order, hue_order, units)
    151                 if isinstance(var, str):
    152                     err = "Could not interpret input '{}'".format(var)
--> 153                     raise ValueError(err)
    154 
    155             # Figure out the plotting orientation

ValueError: Could not interpret input 'caret'
In [ ]:
sns.swarmplot(df.head(1000).carat)
/usr/local/lib/python3.7/dist-packages/seaborn/_decorators.py:43: FutureWarning: Pass the following variable as a keyword arg: x. From version 0.12, the only valid positional argument will be `data`, and passing other arguments without an explicit keyword will result in an error or misinterpretation.
  FutureWarning
/usr/local/lib/python3.7/dist-packages/seaborn/categorical.py:1296: UserWarning: 44.4% of the points cannot be placed; you may want to decrease the size of the markers or use stripplot.
  warnings.warn(msg, UserWarning)
Out[ ]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f4cfbe6cc90>
In [ ]:
sns.swarmplot(df.sample(500).carat)
/usr/local/lib/python3.7/dist-packages/seaborn/_decorators.py:43: FutureWarning: Pass the following variable as a keyword arg: x. From version 0.12, the only valid positional argument will be `data`, and passing other arguments without an explicit keyword will result in an error or misinterpretation.
  FutureWarning
/usr/local/lib/python3.7/dist-packages/seaborn/categorical.py:1296: UserWarning: 13.6% of the points cannot be placed; you may want to decrease the size of the markers or use stripplot.
  warnings.warn(msg, UserWarning)
Out[ ]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f4cfbc49050>
In [ ]:
sns.swarmplot(df.head(500).carat)
/usr/local/lib/python3.7/dist-packages/seaborn/_decorators.py:43: FutureWarning: Pass the following variable as a keyword arg: x. From version 0.12, the only valid positional argument will be `data`, and passing other arguments without an explicit keyword will result in an error or misinterpretation.
  FutureWarning
/usr/local/lib/python3.7/dist-packages/seaborn/categorical.py:1296: UserWarning: 27.0% of the points cannot be placed; you may want to decrease the size of the markers or use stripplot.
  warnings.warn(msg, UserWarning)
Out[ ]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f4cfccf4390>
In [ ]:
df.sample(50).head()
Out[ ]:
carat cut color clarity depth table price x y z
8932 1.01 Premium F SI2 61.6 58.0 4497 6.40 6.39 3.94
50158 0.52 Ideal D VVS2 59.9 57.0 2217 5.21 5.28 3.14
11971 1.01 Ideal F SI1 62.3 55.0 5140 6.37 6.44 3.99
40402 0.55 Ideal J VS2 60.4 58.0 1133 5.29 5.34 3.21
1941 0.90 Fair E SI2 64.5 55.0 3084 6.12 6.03 3.92
In [ ]:
df.sample(5)
Out[ ]:
carat cut color clarity depth table price x y z
6433 1.02 Fair G SI1 64.5 56.0 4044 6.30 6.23 4.04
324 1.00 Premium J SI2 62.3 58.0 2801 6.45 6.34 3.98
20875 1.59 Very Good G SI2 61.7 59.0 9094 7.41 7.46 4.59
46662 0.51 Ideal D VS2 60.9 56.0 1787 5.16 5.19 3.15
18669 1.12 Ideal G VS1 61.0 56.0 7632 6.76 6.72 4.11
In [ ]:
sns.swarmplot(df.sample(1000).price);
/usr/local/lib/python3.7/dist-packages/seaborn/_decorators.py:43: FutureWarning: Pass the following variable as a keyword arg: x. From version 0.12, the only valid positional argument will be `data`, and passing other arguments without an explicit keyword will result in an error or misinterpretation.
  FutureWarning
/usr/local/lib/python3.7/dist-packages/seaborn/categorical.py:1296: UserWarning: 26.0% of the points cannot be placed; you may want to decrease the size of the markers or use stripplot.
  warnings.warn(msg, UserWarning)
In [ ]:
sns.swarmplot(df.sample(1000).carat);
/usr/local/lib/python3.7/dist-packages/seaborn/_decorators.py:43: FutureWarning: Pass the following variable as a keyword arg: x. From version 0.12, the only valid positional argument will be `data`, and passing other arguments without an explicit keyword will result in an error or misinterpretation.
  FutureWarning
/usr/local/lib/python3.7/dist-packages/seaborn/categorical.py:1296: UserWarning: 40.3% of the points cannot be placed; you may want to decrease the size of the markers or use stripplot.
  warnings.warn(msg, UserWarning)

by reducing the sample size to 100, may be more descriptive in graph

In [ ]:
sns.swarmplot(df.sample(100).price)
/usr/local/lib/python3.7/dist-packages/seaborn/_decorators.py:43: FutureWarning: Pass the following variable as a keyword arg: x. From version 0.12, the only valid positional argument will be `data`, and passing other arguments without an explicit keyword will result in an error or misinterpretation.
  FutureWarning
Out[ ]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f4cf73bd710>
In [ ]:
sns.swarmplot(df.sample(100).carat)
/usr/local/lib/python3.7/dist-packages/seaborn/_decorators.py:43: FutureWarning: Pass the following variable as a keyword arg: x. From version 0.12, the only valid positional argument will be `data`, and passing other arguments without an explicit keyword will result in an error or misinterpretation.
  FutureWarning
Out[ ]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f4cf73448d0>
In [ ]:
sns.kdeplot(df.sample(100).carat)
Out[ ]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f4cf8e3d950>
In [ ]:
sns.kdeplot(df.sample(100).price)
Out[ ]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f4cf72b4350>

In JOINT plot we have two continuous variables, where as here,

in SWARM Plots, we can have one continuous and the other to be categorical variable

In [ ]:
df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 53940 entries, 0 to 53939
Data columns (total 10 columns):
 #   Column   Non-Null Count  Dtype   
---  ------   --------------  -----   
 0   carat    53940 non-null  float64 
 1   cut      53940 non-null  category
 2   color    53940 non-null  category
 3   clarity  53940 non-null  category
 4   depth    53940 non-null  float64 
 5   table    53940 non-null  float64 
 6   price    53940 non-null  int64   
 7   x        53940 non-null  float64 
 8   y        53940 non-null  float64 
 9   z        53940 non-null  float64 
dtypes: category(3), float64(6), int64(1)
memory usage: 3.0 MB

sns.jointplot(x, y, data = df)

In [ ]:
df500 = df.sample(500)
In [ ]:
sns.jointplot(x = 'carat', y = 'price', data = df500)
Out[ ]:
<seaborn.axisgrid.JointGrid at 0x7f4cf4fb2dd0>
In [ ]:
sns.kdeplot(x='carat', y='price', data=df500)
Out[ ]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f4cf4cd2f10>
In [ ]:
sns.swarmplot(x='cut', y='price', data=df500);
/usr/local/lib/python3.7/dist-packages/seaborn/categorical.py:1296: UserWarning: 35.9% of the points cannot be placed; you may want to decrease the size of the markers or use stripplot.
  warnings.warn(msg, UserWarning)
/usr/local/lib/python3.7/dist-packages/seaborn/categorical.py:1296: UserWarning: 15.6% of the points cannot be placed; you may want to decrease the size of the markers or use stripplot.
  warnings.warn(msg, UserWarning)
/usr/local/lib/python3.7/dist-packages/seaborn/categorical.py:1296: UserWarning: 14.1% of the points cannot be placed; you may want to decrease the size of the markers or use stripplot.
  warnings.warn(msg, UserWarning)
In [ ]:
sns.boxplot(x='carat', y='price', data=df500)
Out[ ]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f4cf48d9050>
In [ ]:
sns.boxplot(x='carat', data=df500)
Out[ ]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f4cf48e65d0>
In [ ]:
sns.boxplot(x = 'price', data=df500)
Out[ ]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f4cf3d7ee90>
In [ ]:
sns.swarmplot(x='cut', y = 'price', data=df500)
/usr/local/lib/python3.7/dist-packages/seaborn/categorical.py:1296: UserWarning: 35.9% of the points cannot be placed; you may want to decrease the size of the markers or use stripplot.
  warnings.warn(msg, UserWarning)
/usr/local/lib/python3.7/dist-packages/seaborn/categorical.py:1296: UserWarning: 15.6% of the points cannot be placed; you may want to decrease the size of the markers or use stripplot.
  warnings.warn(msg, UserWarning)
/usr/local/lib/python3.7/dist-packages/seaborn/categorical.py:1296: UserWarning: 14.1% of the points cannot be placed; you may want to decrease the size of the markers or use stripplot.
  warnings.warn(msg, UserWarning)
Out[ ]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f4cf3d2ea90>
In [ ]:
df500.head(2)
Out[ ]:
carat cut color clarity depth table price x y z
11751 1.01 Premium E SI1 63.0 61.0 5077 6.42 6.37 4.03
46108 0.55 Very Good G VS1 62.8 57.0 1743 5.20 5.21 3.27
In [ ]:
sns.swarmplot(x='color', y='price', data=df500)
/usr/local/lib/python3.7/dist-packages/seaborn/categorical.py:1296: UserWarning: 13.6% of the points cannot be placed; you may want to decrease the size of the markers or use stripplot.
  warnings.warn(msg, UserWarning)
/usr/local/lib/python3.7/dist-packages/seaborn/categorical.py:1296: UserWarning: 33.7% of the points cannot be placed; you may want to decrease the size of the markers or use stripplot.
  warnings.warn(msg, UserWarning)
/usr/local/lib/python3.7/dist-packages/seaborn/categorical.py:1296: UserWarning: 26.4% of the points cannot be placed; you may want to decrease the size of the markers or use stripplot.
  warnings.warn(msg, UserWarning)
/usr/local/lib/python3.7/dist-packages/seaborn/categorical.py:1296: UserWarning: 28.9% of the points cannot be placed; you may want to decrease the size of the markers or use stripplot.
  warnings.warn(msg, UserWarning)
/usr/local/lib/python3.7/dist-packages/seaborn/categorical.py:1296: UserWarning: 24.1% of the points cannot be placed; you may want to decrease the size of the markers or use stripplot.
  warnings.warn(msg, UserWarning)
Out[ ]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f4cf3cce210>
In [ ]:
sns.swarmplot(x="clarity", y = 'price', data=df500)
/usr/local/lib/python3.7/dist-packages/seaborn/categorical.py:1296: UserWarning: 23.8% of the points cannot be placed; you may want to decrease the size of the markers or use stripplot.
  warnings.warn(msg, UserWarning)
/usr/local/lib/python3.7/dist-packages/seaborn/categorical.py:1296: UserWarning: 25.0% of the points cannot be placed; you may want to decrease the size of the markers or use stripplot.
  warnings.warn(msg, UserWarning)
/usr/local/lib/python3.7/dist-packages/seaborn/categorical.py:1296: UserWarning: 27.9% of the points cannot be placed; you may want to decrease the size of the markers or use stripplot.
  warnings.warn(msg, UserWarning)
/usr/local/lib/python3.7/dist-packages/seaborn/categorical.py:1296: UserWarning: 30.9% of the points cannot be placed; you may want to decrease the size of the markers or use stripplot.
  warnings.warn(msg, UserWarning)
/usr/local/lib/python3.7/dist-packages/seaborn/categorical.py:1296: UserWarning: 27.1% of the points cannot be placed; you may want to decrease the size of the markers or use stripplot.
  warnings.warn(msg, UserWarning)
/usr/local/lib/python3.7/dist-packages/seaborn/categorical.py:1296: UserWarning: 14.9% of the points cannot be placed; you may want to decrease the size of the markers or use stripplot.
  warnings.warn(msg, UserWarning)
Out[ ]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f4cf3c6c490>

Penguins dataset from Seaborn

In [ ]:
pen = sns.load_dataset('penguins')
In [ ]:
pen.head()
Out[ ]:
species island bill_length_mm bill_depth_mm flipper_length_mm body_mass_g sex
0 Adelie Torgersen 39.1 18.7 181.0 3750.0 Male
1 Adelie Torgersen 39.5 17.4 186.0 3800.0 Female
2 Adelie Torgersen 40.3 18.0 195.0 3250.0 Female
3 Adelie Torgersen NaN NaN NaN NaN NaN
4 Adelie Torgersen 36.7 19.3 193.0 3450.0 Female
In [ ]:
sns.swarmplot(x=pen.species, y = pen.bill_length_mm, data = pen)
Out[ ]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f4cf1e5a090>
In [ ]:
sns.swarmplot(x=pen.island, y=pen.bill_length_mm, data=pen )
Out[ ]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f4cf3b8d990>
In [ ]:
sns.swarmplot(x='island', y='bill_length_mm', data=pen, hue='species')
Out[ ]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f4cf1cbf610>
In [ ]:
sns.swarmplot(x='species', y='bill_length_mm', data=pen, hue='island')
Out[ ]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f4cf1c131d0>
In [ ]:
sns.swarmplot(x='species', y='body_mass_g', data=pen)
Out[ ]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f4cf15fe7d0>
In [ ]:
sns.swarmplot(x='body_mass_g', data=pen)
Out[ ]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f4cf1545110>

Strip Plots in Seaborn

In [ ]:
sns.stripplot(x='island', y='bill_length_mm', data = pen, color='0.3')
Out[ ]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f4cf18e0fd0>
In [ ]:
sns.boxplot(x='island', y='bill_length_mm', data = pen)
Out[ ]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f4cf1bb5c10>

both Box plots and Strip plots together...

In [ ]:
sns.boxplot(x='island', y='bill_length_mm', data = pen)
sns.stripplot(x='island', y='bill_length_mm', data = pen, color='0.3')
Out[ ]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f4cf1afdfd0>
In [ ]:
sns.boxplot(x='bill_length_mm', y='island', data = pen)
sns.stripplot(x='bill_length_mm', y='island', data = pen, color = '0.3')
Out[ ]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f4cf0fa5b90>
In [ ]:
flights_wide = sns.load_dataset("flights").pivot("year", "month", "passengers")
In [ ]:
flights_wide.head()
Out[ ]:
month Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
year
1949 112 118 132 129 121 135 148 148 136 119 104 118
1950 115 126 141 135 125 149 170 170 158 133 114 140
1951 145 150 178 163 172 178 199 199 184 162 146 166
1952 171 180 193 181 183 218 230 242 209 191 172 194
1953 196 196 236 235 229 243 264 272 237 211 180 201
In [ ]:
sns.relplot(data=flights_wide, kind='line')  # optios for kind are LINE and  SCATTER
Out[ ]:
<seaborn.axisgrid.FacetGrid at 0x7f4cf16de890>
In [ ]:
sns.relplot(data=flights_wide, kind='scatter')
Out[ ]:
<seaborn.axisgrid.FacetGrid at 0x7f4cf178f0d0>
In [ ]:
sns.relplot(x='species', y='bill_length_mm', kind='line', data = pen, hue='island')
Out[ ]:
<seaborn.axisgrid.FacetGrid at 0x7f4cf1292e90>
In [ ]:
pen.head(2)
Out[ ]:
species island bill_length_mm bill_depth_mm flipper_length_mm body_mass_g sex
0 Adelie Torgersen 39.1 18.7 181.0 3750.0 Male
1 Adelie Torgersen 39.5 17.4 186.0 3800.0 Female
In [ ]:
sns.stripplot(x='bill_length_mm', y='island', data = pen, color = '0.3')
Out[ ]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f4cf0e824d0>
In [ ]:
sns.stripplot(x='bill_length_mm', y='island', data = pen, color = '0.3')
sns.boxplot(x='bill_length_mm', y='island', data = pen)
Out[ ]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f4cf0bba710>
In [ ]: