SEABORN - BAR & LINE PLOTS Padhai ClassWork
dv2_BarLinePlot

BAR PLOT AND LINE PLOTS

Bar Plot to see relationship between continuous and categorical variables

seaborn.barplot(*, x=None, y=None, hue=None, data=None, order=None, hue_order=None, estimator=<function mean at 0x7ff320f315e0>, ci=95, n_boot=1000, units=None, seed=None, orient=None, color=None, palette=None, saturation=0.75, errcolor='.26', errwidth=None, capsize=None, dodge=True, ax=None, **kwargs)

https://seaborn.pydata.org/generated/seaborn.barplot.html

In [ ]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
plt.style.use(['dark_background'])
import seaborn as sns
sns.set(color_codes = True)
  • fig = plt.gcf()
  • fig.set_size_inches(8,6)
  • plt.legend(bbox_to_anchor = (1,1))
In [ ]:
t = sns.load_dataset("tips")
In [ ]:
t.head()
Out[ ]:
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

pandas groupby is working with this bar plot

In [ ]:
sns.barplot(x="day", y='tip', data=t)
Out[ ]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f1958503150>
In [ ]:
t['tip_fraction'] = t['tip']/t['total_bill']
In [ ]:
t.head(2)
Out[ ]:
total_bill tip sex smoker day time size tip_fraction
0 16.99 1.01 Female No Sun Dinner 2 0.059447
1 10.34 1.66 Male No Sun Dinner 3 0.160542
In [ ]:
sns.barplot(x='day', y = 'tip_fraction', data = t)
Out[ ]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f1956e0aa10>
In [ ]:
sns.barplot(x="day", y='tip', data=t, estimator = np.median)
Out[ ]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f1956943650>
In [ ]:
def my_estimate(v):
    return np.quantile(v, 0.25)
In [ ]:
sns.barplot(x="day", y='tip', data=t, estimator = my_estimate)
Out[ ]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f19568c5410>
In [ ]:
sns.barplot(x="day", y='tip', hue = 'sex', data=t, estimator = np.median)
Out[ ]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f1956833750>
In [ ]:
sns.barplot(x="day", y='tip', hue = 'smoker', data=t, estimator = np.median)
Out[ ]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f1956762dd0>
In [ ]:
sns.barplot(x="day", y='tip', hue = 'time', data=t, estimator = np.median)
Out[ ]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f1956700b50>
In [ ]:
sns.barplot(x="day", y='tip_fraction', hue = 'sex', data=t, estimator = np.median)
Out[ ]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f195661c5d0>
In [ ]:
sns.barplot(x = 'time', y='tip', data = t, order=['Dinner', 'Lunch'])
Out[ ]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f19565bf250>
In [ ]:
sns.barplot(x="size", y="total_bill", data=t, palette="Blues_d")
Out[ ]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f1956583990>
In [ ]:
t['weekend'] = t['day'].isin (['Sat', 'Sun'])
In [ ]:
t.head()
Out[ ]:
total_bill tip sex smoker day time size tip_fraction weekend
0 16.99 1.01 Female No Sun Dinner 2 0.059447 True
1 10.34 1.66 Male No Sun Dinner 3 0.160542 True
2 21.01 3.50 Male No Sun Dinner 3 0.166587 True
3 23.68 3.31 Male No Sun Dinner 2 0.139780 True
4 24.59 3.61 Female No Sun Dinner 4 0.146808 True
In [ ]:
sns.barplot(x="day", y="total_bill", hue="weekend",
                 data=t, dodge=False)
Out[ ]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f19564abb50>
In [ ]:
sns.catplot(x="sex", y="total_bill",
                hue="smoker", col="time",
                data=t, kind="bar",
                height=4, aspect=.7);
In [ ]:
d = sns.load_dataset('diamonds')
In [ ]:
d.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.scatterplot('x', y='price', data=d.sample(1000))
/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 0x7f1956619490>
In [ ]:
sns.barplot('x', y='price', data=d.sample(1000))
/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 0x7f19539dcb90>

CUT f() is used to discretise a continuous col

In [ ]:
d['x_q'] = pd.cut(d['x'], bins = 15)
In [ ]:
d['x_q'].unique()
Out[ ]:
[(3.58, 4.296], (4.296, 5.012], (5.012, 5.728], (6.444, 7.16], (5.728, 6.444], ..., (-0.0107, 0.716], (7.876, 8.592], (8.592, 9.308], (9.308, 10.024], (10.024, 10.74]]
Length: 11
Categories (15, interval[float64, right]): [(-0.0107, 0.716] < (0.716, 1.432] < (1.432, 2.148] <
                                            (2.148, 2.864] ... (7.876, 8.592] < (8.592, 9.308] <
                                            (9.308, 10.024] < (10.024, 10.74]]
In [ ]:
d.head(2)
Out[ ]:
carat cut color clarity depth table price x y z x_q
0 0.23 Ideal E SI2 61.5 55.0 326 3.95 3.98 2.43 (3.58, 4.296]
1 0.21 Premium E SI1 59.8 61.0 326 3.89 3.84 2.31 (3.58, 4.296]
In [ ]:
sns.barplot('x_q', y='price', data=d)
/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 0x7f1956700310>
In [ ]:
d['x_q'] = pd.cut(d['x'], bins = 15, labels = False)
In [ ]:
sns.barplot('x_q', y='price', data=d)
/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 0x7f1952e39f50>

SEABORN LINE PLOT

FMRI dataset - MRI done for brain region

In [ ]:
f = sns.load_dataset("fmri")
In [ ]:
f.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 1064 entries, 0 to 1063
Data columns (total 5 columns):
 #   Column     Non-Null Count  Dtype  
---  ------     --------------  -----  
 0   subject    1064 non-null   object 
 1   timepoint  1064 non-null   int64  
 2   event      1064 non-null   object 
 3   region     1064 non-null   object 
 4   signal     1064 non-null   float64
dtypes: float64(1), int64(1), object(3)
memory usage: 41.7+ KB
In [ ]:
f.describe()
Out[ ]:
timepoint signal
count 1064.000000 1064.000000
mean 9.000000 0.003540
std 5.479801 0.093930
min 0.000000 -0.255486
25% 4.000000 -0.046070
50% 9.000000 -0.013653
75% 14.000000 0.024293
max 18.000000 0.564985
In [ ]:
f.head()
Out[ ]:
subject timepoint event region signal
0 s13 18 stim parietal -0.017552
1 s5 14 stim parietal -0.080883
2 s12 18 stim parietal -0.081033
3 s11 18 stim parietal -0.046134
4 s10 18 stim parietal -0.037970
In [ ]:
f['region'].unique()
Out[ ]:
array(['parietal', 'frontal'], dtype=object)
In [ ]:
sns.lineplot('timepoint', 'signal', data=f)
/usr/local/lib/python3.7/dist-packages/seaborn/_decorators.py:43: FutureWarning: Pass the following variables as keyword args: x, y. 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 0x7f1950bfc110>
In [ ]:
f['timepoint'].unique()
Out[ ]:
array([18, 14, 17,  9, 16, 15,  0, 13, 12, 11, 10,  3,  7,  8,  2,  6,  5,
        4,  1])
In [ ]:
f['event'].unique()
Out[ ]:
array(['stim', 'cue'], dtype=object)
In [ ]:
sns.lineplot('timepoint', 'signal', data=f, hue='region')
/usr/local/lib/python3.7/dist-packages/seaborn/_decorators.py:43: FutureWarning: Pass the following variables as keyword args: x, y. 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 0x7f1950c578d0>
In [ ]:
sns.lineplot('timepoint', 'signal', data=f, hue='event')
/usr/local/lib/python3.7/dist-packages/seaborn/_decorators.py:43: FutureWarning: Pass the following variables as keyword args: x, y. 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 0x7f1950a558d0>
In [ ]:
sns.lineplot('timepoint', 'signal', data=f, hue='event', style = 'region')
/usr/local/lib/python3.7/dist-packages/seaborn/_decorators.py:43: FutureWarning: Pass the following variables as keyword args: x, y. 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 0x7f1950a3fbd0>
In [ ]:
sns.lineplot('timepoint', 'signal', data=f, hue='region', marker = True)
/usr/local/lib/python3.7/dist-packages/seaborn/_decorators.py:43: FutureWarning: Pass the following variables as keyword args: x, y. 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 0x7f19508a75d0>
In [ ]:
sns.lineplot('timepoint', 'signal', data=f, hue='region', estimator = np.median)
/usr/local/lib/python3.7/dist-packages/seaborn/_decorators.py:43: FutureWarning: Pass the following variables as keyword args: x, y. 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 0x7f1950853450>
In [ ]:
f['subject'].unique()
Out[ ]:
array(['s13', 's5', 's12', 's11', 's10', 's9', 's8', 's7', 's6', 's4',
       's3', 's2', 's1', 's0'], dtype=object)
In [ ]:
sns.lineplot('timepoint', 'signal', data = f, units = 'subject', estimator = None)
/usr/local/lib/python3.7/dist-packages/seaborn/_decorators.py:43: FutureWarning: Pass the following variables as keyword args: x, y. 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 0x7f19505b8a90>

creating subset from the given dataset

In [ ]:
f_ = f[(f['region'] == 'parietal') & (f['event'] == 'cue')]
In [ ]:
f_.head()
Out[ ]:
subject timepoint event region signal
532 s3 4 cue parietal 0.058219
533 s6 5 cue parietal 0.038145
534 s7 5 cue parietal -0.008158
535 s8 5 cue parietal 0.047136
536 s9 5 cue parietal 0.055847
In [ ]:
sns.lineplot('timepoint', 'signal', data = f_, units = 'subject', estimator = None)
/usr/local/lib/python3.7/dist-packages/seaborn/_decorators.py:43: FutureWarning: Pass the following variables as keyword args: x, y. 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 0x7f19504fb9d0>
In [ ]:
sns.lineplot('timepoint', 'signal', data = f_, hue = 'subject', estimator = None)
/usr/local/lib/python3.7/dist-packages/seaborn/_decorators.py:43: FutureWarning: Pass the following variables as keyword args: x, y. 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 0x7f1950497590>
In [ ]:
sns.lineplot('timepoint', 'signal', data = f_, hue = 'subject', estimator = None)
/usr/local/lib/python3.7/dist-packages/seaborn/_decorators.py:43: FutureWarning: Pass the following variables as keyword args: x, y. 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 0x7f194ea5e610>
In [ ]:
x = np.array([-1,-2,-3,-4,-5,0,1,2,3,4,5])
y = x * x
In [ ]:
sns.lineplot(x, y)
/usr/local/lib/python3.7/dist-packages/seaborn/_decorators.py:43: FutureWarning: Pass the following variables as keyword args: x, y. 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 0x7f194e92d210>
In [ ]:
!pip install nbconvert
In [ ]:
%shell jupyter nbconvert --to html /content/testfile.ipynb