Visualization Seaborn#

Section Title: Visualization Seaborn

In this section, we’ll use seaborn, importing it as sns.

import seaborn as sns

We will use the following lists:

expenses = ['housing', 'tuition', 'transportation', 'supplies', 'food']
cost = [700, 1000, 200, 100, 500 ]
color_set = ['y', 'r', 'g', 'orange', 'navy']

Scatterplot#

sns.scatterplot(x=[2,4,7],y=[10,4,6], color='r', s=100);
_images/550e832835c90236ab42409da8db270f5503089e8c2ae4194f7266ca3c377744.png

Lineplot#

sns.lineplot(x=[2,4,7],y=[10,4,6], color='r');
_images/73f13a5c5c1d898ba74371e4deb7482c8827f129a9a44af41fff3f1e5f83160e.png

Barplot#

sns.barplot(x=cost, y=expenses);
_images/a88d71717b5bc0e6bc5ebd383303445b88df4eef523d303ad17c6e5f347cbfb3.png

Box Plot#

sns.boxplot([8,7,3,2,1,5,6,7,8]);
_images/f22f49017895f33a2eaba0a614f4d56ec7e7141b60be95593d9d49d26f7e0811.png

Displot#

  • sns.displot() is used to generate histograms.

sns.displot([8,7,3,2,1,5,6,7,8,1,3,2,5]);
_images/fb279d59d0acc0a03ada5523a1d657c78bd4f4b6105dc1807a3d00e7092aeca7.png

Histplot#

sns.histplot() is used to generate histograms.

sns.histplot([8,7,3,2,1,5,6,7,8,1,3,2,5]);
_images/9c3562b256523fc4cc521774fe399481f0909d26d86f254567a541680c735328.png

Violinplot#

sns.violinplot() is used to generate violinplots, which represent the distribution of the data.

sns.violinplot([8,7,3,2,1,5,6,7,8,1,3,2,5]);
_images/64e1e32506b8792e6e4f96ea2ddbca371a4ef92496688d1a32ce30543a4cb8c9.png

Swarmplot#

sns.swarmplot() is used to generate swarmplots, which represent the distribution of the data.

sns.swarmplot([8,7,3,2,1,5,6,7,8,1,3,3,3,3,3,3,3,3,3,2,5,5,5,5,5,5,5,5]);
_images/ddb76e5f2d11082cfca9812fcf7e584baf5f61e0397c12e58c9a5b880a4c6d61.png
sns.swarmplot([8,7,3,2,1,5,6,7,8,1,3,3,3,3,3,3,3,3,3,2,5,5,5,5,5,5,5,5], c='yellow');
sns.violinplot([8,7,3,2,1,5,6,7,8,1,3,3,3,3,3,3,3,3,3,2,5,5,5,5,5,5,5,5], color='red');
_images/47d038ece0d8fe5231affeb0ee146d5399b5aaa5fc658e33348ff0b44b298e68.png

DataFrames#

We will use the following dataset to perform visualization with Seaborn on a dataframe.

import pandas as pd
df_stock = pd.read_excel('https://raw.githubusercontent.com/datasmp/datasets/main/stock.xlsx')
df_stock.head()
Date APPLE TESLA AMAZON VISA SP500
0 2020-01-02 74.33 86.05 1898.01 189.66 3257.85
1 2020-01-03 73.61 88.60 1874.97 188.15 3234.85
2 2020-01-06 74.20 90.31 1902.88 187.74 3246.28
3 2020-01-07 73.85 93.81 1906.86 187.24 3237.18
4 2020-01-08 75.04 98.43 1891.97 190.45 3253.05
import seaborn as sns
sns.boxplot(data=df_stock);
_images/cd174b6721880d302716604ea78db602bf182a4d715ced1feaa9175f5206b93b.png
# apple
sns.boxplot(data=df_stock['APPLE']);
_images/324350e130cc0119bc25a118fa9ac2ae47660fbb18b136502c3d60e04e813ae2.png
sns.violinplot(data=df_stock.iloc[:,1:]);
_images/2ef0cf7666d4e7121d5aabc09950ac5f93a5efa99419bae613dbb544af90e5e8.png
sns.violinplot(data=df_stock['APPLE']);
_images/430336308d0d6433df6e3ead3ae3641673fb0ef09d58890ebc6670ae16726efa.png
sns.swarmplot(data=df_stock['AMAZON']);
_images/f0363945d62c037f00b2044332a5ac2f56f22cabdb2373530c98d08f2e42c933.png
sns.violinplot(data=df_stock['TESLA']);
_images/d740f715931250057f7505243b848b718bc801db8f880d46a0abca55f81468b6.png
sns.violinplot(data=df_stock['TESLA']);
sns.swarmplot(data=df_stock['TESLA'], color='orange');
_images/b6f9b43aa24cf0fcb86061f668c5a6f01b7f7e56422cc009f1e10c9ce4d9f720.png