0% found this document useful (0 votes)
3 views24 pages

Data Visualization Project

The document provides a comprehensive guide on using Seaborn for data visualization, including setup, data import, and various plotting techniques such as distribution plots, categorical plots, and matrix plots. It covers specific functions like jointplot, pairplot, and lmplot, along with styling options and customization of plots. The document also includes examples using datasets like 'car_crashes' and 'tips' to illustrate different visualization methods.

Uploaded by

gimoke4583
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views24 pages

Data Visualization Project

The document provides a comprehensive guide on using Seaborn for data visualization, including setup, data import, and various plotting techniques such as distribution plots, categorical plots, and matrix plots. It covers specific functions like jointplot, pairplot, and lmplot, along with styling options and customization of plots. The document also includes examples using datasets like 'car_crashes' and 'tips' to illustrate different visualization methods.

Uploaded by

gimoke4583
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Learn_Seaborn

January 18, 2023

0.1 Setup
[2]: import numpy as np
import pandas as pd
import seaborn as sns
import [Link] as plt

%reload_ext autoreload
%autoreload 2
%matplotlib inline

0.2 Import Data


[3]: # df = pd.read_csv()
crash_df = sns.load_dataset('car_crashes')
crash_df.head(10)

[3]: total speeding alcohol not_distracted no_previous ins_premium \


0 18.8 7.332 5.640 18.048 15.040 784.55
1 18.1 7.421 4.525 16.290 17.014 1053.48
2 18.6 6.510 5.208 15.624 17.856 899.47
3 22.4 4.032 5.824 21.056 21.280 827.34
4 12.0 4.200 3.360 10.920 10.680 878.41
5 13.6 5.032 3.808 10.744 12.920 835.50
6 10.8 4.968 3.888 9.396 8.856 1068.73
7 16.2 6.156 4.860 14.094 16.038 1137.87
8 5.9 2.006 1.593 5.900 5.900 1273.89
9 17.9 3.759 5.191 16.468 16.826 1160.13

ins_losses abbrev
0 145.08 AL
1 133.93 AK
2 110.35 AZ
3 142.39 AR
4 165.63 CA
5 139.91 CO
6 167.02 CT
7 151.48 DE

1
8 136.05 DC
9 144.18 FL

1 Distribution Plots
1.1 Distribution Plot
[4]: # [Link](crash_df['not_distracted'])
[Link](crash_df['not_distracted'],kde=False,bins=50)
[Link]()

1.2 Joint Plot


[5]: # compare two distribution and plot a scatter plot by default
[Link](x='speeding',y='alcohol',data=crash_df,kind='kde') # king =␣
↪['kde','hex','reg']

[Link]()

2
1.3 KDE Plot
[6]: [Link](crash_df['alcohol'])
[Link]()

3
1.4 Pair Plot
[7]: # It plot the the relationship across entire dataframe and numerical values
# [Link](crash_df)
tips_df = sns.load_dataset('tips')
[Link](tips_df,hue='sex',palette='Blues') # hue :-> with hue we can pass␣
↪the categorial data and chart will be colorised based on that data

[Link]()

4
1.5 Rug Plot
[8]: # It plots the a sigle column of datapoints in a dataframe as sticks on the x␣
↪axis, it show more dense number of lines where the amount is more common

[Link](tips_df['tip'])
[Link]()

5
1.6 Styling
[9]: sns.set_style('ticks') # style= ['white','whitegrid',dark,ticks,'darkgrid']
sns.set_context('paper',font_scale=1.0) # [paper,talk,poster]
[Link](figsize=(2,2))
[Link](x='speeding',y='alcohol',data=crash_df,kind='reg')

# [Link](left=True,bottom=True) # [right, top]


[Link]()

<Figure size 144x144 with 0 Axes>

6
2 Categorial Plots
2.1 Bar Plot
[10]: [Link](x='sex',y='total_bill',data=tips_df,estimator=[Link]) #␣
↪estimator=[median,mean(default),[Link](StandardDeviation), np.

↪var(varience),[Link](coverience),custom]

[Link]()

7
2.2 Count Plot
[11]: # kind of bar plot but estimator is just count the number of occurances
[Link](x='sex',data=tips_df)

[11]: <[Link]._subplots.AxesSubplot at 0x7ff4add963a0>

8
2.3 Box Plot
[12]: # A box plot is going to compare the different variables qurtile of the data
[Link](x='day',y='total_bill',data=tips_df,hue='sex')

[12]: <[Link]._subplots.AxesSubplot at 0x7ff4add9f460>

9
2.4 Violin Plot
[13]: # it is combination of box plot and a kde plot
# a violin plot uses the kde estimation of data points
[Link](x='day',y='total_bill',data=tips_df,hue='sex',split=True)

[13]: <[Link]._subplots.AxesSubplot at 0x7ff4b1b82280>

10
2.5 Strip Plot
[14]: # strip plot draws the scatter plot represents all data points where one␣
↪variable is categorical

[Link](figsize=(10,6))
[Link](x='day',y='total_bill',data=tips_df,jitter=True,hue='sex') #␣
↪,dodge=True :-> seprates the data

[Link]()

11
2.6 Swarm Plot
[15]: # It is a combination of violin and a strip plot
[Link](figsize=(12,10))
[Link](x='day',y='total_bill',data=tips_df)
[Link](x='day',y='total_bill',data=tips_df,color='white',hue='sex')
[Link]()

12
2.7 Palettes
• by setting the palettes you can change the default coloring of the any plot
• to see more detail about the color palette click here

[16]: # by setting the palettes you can change the default coloring of the any plot
[Link](figsize=(12,9))
sns.set_style('white')
sns.set_context('talk')
[Link](x='day',y='total_bill',data=tips_df,hue='sex',palette='afmhot')
# [Link](loc=1)
[Link]()

13
3 Matrix Plot
3.1 Heatmaps
[17]: #
[Link](figsize=(8,7))
sns.set_context('paper',font_scale=1.4)

crash_mx = crash_df.corr()

[Link](crash_mx,annot=True,cmap='Blues',linewidth=1,linecolor='black')
[Link]()

14
[18]: [Link](figsize=(12,10))
flights_df = sns.load_dataset('flights')
flights = flights_df.
↪pivot_table(index='month',columns='year',values='passengers')

[Link](flights,cmap='Blues',linecolor='white',linewidth=1)
[Link]()

15
3.2 Cluster Map
[19]: # it gives the cluster data by finding the closest distance between various␣
↪points

iris = sns.load_dataset('iris')
# species = [Link]('species')
# [Link](iris)

[20]: [Link](flights,cmap='Blues',standard_scale=1)

[20]: <[Link] at 0x7ff4adc3b670>

16
3.3 Pair Grid
[21]: # to plot or change the default plots in the different position use the␣
↪'shift+tab' [iris_g.map_upper,.map_lower,.map_offdiag,...]

iris_g = [Link](iris,hue='species')
# iris_g.map([Link])
iris_g.map_diag([Link])
iris_g.map_offdiag([Link])

[21]: <[Link] at 0x7ff4b1c02c70>

17
[22]: # for our custom grid plot we can define the x and y variables with dataset
iris_g = sns.
↪PairGrid(iris,hue='species',x_vars=['sepal_length','sepal_width'],y_vars=['petal_length','pe

iris_g.map([Link])
iris_g.add_legend()

[22]: <[Link] at 0x7ff4addad880>

18
3.4 Facet Grid
[23]: import seaborn as sns
# this can plot multiple plot in a grid and in this we can define the columns␣
↪and rows

tips_fg = [Link](tips_df,col='time',row='smoker') # to draw the plot fig␣


↪with only axis no data in it

# sns.set_context('paper',font_scale=1.2)
tips_fg.map([Link], 'total_bill',bins=9) # ['time','smoder',]
# tips_fg.map([Link], 'total_bill','tip')

[23]: <[Link] at 0x7ff4ad7d3b20>

19
[24]: tips_fg1 = [Link](tips_df,col='time',hue='smoker',height=6,aspect=1.4)
tips_fg1.map([Link],'total_bill','tip')

[24]: <[Link] at 0x7ff4adf1a7c0>

20
[25]: tips_fg2 = [Link](tips_df,col='time',hue='smoker',height=6,aspect=1.
↪4,col_order=['Dinner','Lunch'],palette='Set1')

tips_fg2.map([Link],'total_bill','tip',edgecolor='b')

[25]: <[Link] at 0x7ff4ad5e6e80>

[26]: kws = dict(s=90,linewidth=1.5,edgecolor='w')


tips_fg2 = [Link](tips_df,col='sex',hue='smoker',height=4,aspect=1.
↪4,hue_order=['Yes','No'],hue_kws=dict(marker=['v','^']))

tips_fg2.map([Link],'total_bill','tip',**kws)

[26]: <[Link] at 0x7ff4ad553df0>

21
3.5 Regression Plot
[27]: [Link](figsize=(8,6))
sns.set_context('paper',font_scale=1.4)
sns.
↪lmplot(x='total_bill',y='tip',hue='sex',data=tips_df,markers=['o','v'],scatter_kws={'s':

↪100,'linewidth':0.5,'edgecolor':'b'})

[27]: <[Link] at 0x7ff4ad5ba460>

<Figure size 576x432 with 0 Axes>

22
[28]: sns.
↪lmplot(x='total_bill',y='tip',col='sex',row='time',data=tips_df,height=8,aspect=1.

↪7)

[28]: <[Link] at 0x7ff4ad4743d0>

23
24

You might also like