[go: up one dir, main page]

0% found this document useful (0 votes)
92 views67 pages

Seaborn Final

The document discusses various visualization techniques using the seaborn library in Python. It covers countplots, barplots, boxplots, violinplots, and stripplots. For each plot type, it provides code examples to create a basic plot, customize features like colors, add additional variables, and change plot parameters. The code examples use the supermarket sales dataset to demonstrate different seaborn visualizations for analyzing the data.

Uploaded by

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

Seaborn Final

The document discusses various visualization techniques using the seaborn library in Python. It covers countplots, barplots, boxplots, violinplots, and stripplots. For each plot type, it provides code examples to create a basic plot, customize features like colors, add additional variables, and change plot parameters. The code examples use the supermarket sales dataset to demonstrate different seaborn visualizations for analyzing the data.

Uploaded by

Sanjid Jaman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 67

SEABORN

hune

uidsjhfjkhkd
1.Seaborn_Introduction.

#!/usr/bin/env python
# coding: utf-8

# **1.Installing Seaborn**

# In[ ]:
conda install seaborn

# In[ ]:
pip install seaborn

# **2.Importing all the required libraries**

# In[77]:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

# **3.Import the data**


# In[76]:
mart = pd.read_excel("D:/Learnerea/Tables/supermarket_sales.xlsx")
mart.head()

# **4.Create a COUNTPLOT**

# In[78]:
sns.countplot(x='Gender',data=mart)

2.Seaborn_CountPlot
#!/usr/bin/env python
# coding: utf-8

# **1.Importing all the required libraries**

# In[80]:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

# **2.Import the data**

# In[81]:

mart = pd.read_excel("D:/Learnerea/Tables/supermarket_sales.xlsx")
mart.head()

# **3.Create a basic COUNTPLOT to get number of transaction for


each of the product line and change the figure size**

# In[83]:
plt.figure(figsize=(15,5))

sns.countplot(x="Product line",data=mart)

# **4.Make it horizontal bar plot**

# In[84]:

sns.countplot(y="Product line",data=mart)

# **5.Add hue to get the count on two categories i.e. Product line and
Gender**

# In[86]:

plt.figure(figsize=(15,5))

sns.countplot(x="Product line",hue="Gender",data=mart)

# **6.Using different color palette**

# In[90]:
plt.figure(figsize=(15,5))

sns.countplot(x="Product line",hue="Gender",data=mart,palette='PuBuGn_r')

# **7.Change Style using facecolor, linewidth and edge color**

# In[91]:

plt.figure(figsize=(15,5))

sns.countplot(x='Product line',data=mart
,facecolor = (0,0,0,0)
,linewidth=5
,edgecolor=sns.color_palette('dark',3))
3. Seaborn_BarPlot

#!/usr/bin/env python
# coding: utf-8

# **1. Import all the required libraries**

# In[106]:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

# **2. Display the SEBORN dataset names and load one of them for
example**

# In[109]:
# sns.get_dataset_names()
titanic=sns.load_dataset('titanic')
titanic.head()

# **3. Import mart dataframe**

# In[110]:

mart = pd.read_excel("D:/Learnerea/Tables/supermarket_sales.xlsx")
mart.head()

# **4. Create a basic BARPLOT**

# In[112]:

plt.figure(figsize=(15,5))

sns.barplot(x='Product line',y='Total',data=mart)

# **5. Add HUE to the barplot**

# In[113]:
plt.figure(figsize=(15,5))

sns.barplot(x='Product line',y='Total',hue='Gender',data=mart)

# **6. Make the barplot HORIZONTAL**

# In[114]:

plt.figure(figsize=(15,5))

sns.barplot(x='Total',y='Product line',data=mart)

# **7. Plot the bars in a given ORDER**

# In[120]:

plt.figure(figsize=(15,5))

sns.barplot(x='Product line',y='Total',hue='Gender',data=mart,order=['Electronic_accessories',
'Fashion_accessories','Food_and_beverages', 'Health_and_beauty',
'Home_and_lifestyle','Sports_and_travel']
,hue_order=['Male','Female'])

# In[117]:
x=mart['Product line'].sort_values()
x.unique()

# **8. Add CAP on the ERROR BAR**

# In[121]:

plt.figure(figsize=(15,5))

sns.barplot(x='Product line',y='Total',hue='Gender',data=mart,order=['Electronic_accessories',
'Fashion_accessories','Food_and_beverages', 'Health_and_beauty',
'Home_and_lifestyle','Sports_and_travel']
,hue_order=['Male','Female']
,capsize=0.2)

# **9. REMOVE the error bar using ci**

# In[122]:

plt.figure(figsize=(15,5))

sns.barplot(x='Product line',y='Total',hue='Gender',data=mart,order=['Electronic_accessories',
'Fashion_accessories','Food_and_beverages', 'Health_and_beauty',
'Home_and_lifestyle','Sports_and_travel']
,hue_order=['Male','Female']
,ci=None)
# **10. Change bar colors using COLOR attribute**

# In[125]:

plt.figure(figsize=(15,5))

sns.barplot(x='Product line',y='Total',data=mart,order=['Electronic_accessories',
'Fashion_accessories','Food_and_beverages', 'Health_and_beauty',
'Home_and_lifestyle','Sports_and_travel']
,hue_order=['Male','Female']
,color='red')

# **11. Change color using PALLETE attribute**

# In[128]:

plt.figure(figsize=(15,5))

sns.barplot(x='Product line',y='Total',hue='Gender',data=mart,order=['Electronic_accessories',
'Fashion_accessories','Food_and_beverages', 'Health_and_beauty',
'Home_and_lifestyle','Sports_and_travel']
,palette='cividis')

# **12. Using SATURATION parameter**


# In[131]:

plt.figure(figsize=(15,5))

sns.barplot(x='Product line',y='Total',hue='Gender',data=mart,order=['Electronic_accessories',
'Fashion_accessories','Food_and_beverages', 'Health_and_beauty',
'Home_and_lifestyle','Sports_and_travel']
,palette='cividis'
,saturation=0.1)

# **13. CHANGE DEFAULT AGGREGATION METHOD USING


ESTIMATOR PARAMETER**

# In[133]:

plt.figure(figsize=(15,5))

sns.barplot(x='Product line',y='Total',hue='Gender',data=mart,order=['Electronic_accessories',
'Fashion_accessories','Food_and_beverages', 'Health_and_beauty',
'Home_and_lifestyle','Sports_and_travel']
,palette='cividis'
,estimator=np.median)
4.Seaborn_BoxPlot

#!/usr/bin/env python
# coding: utf-8

# **1. Import all the required LIBRARIES**

# In[24]:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

# **2. Import MART dataframe**

# In[25]:

mart = pd.read_excel("D:/Learnerea/Tables/supermarket_sales.xlsx")
mart.head()

# **3. Create a basic BOX plot on one NUMERIC variable**

# In[29]:

sns.boxplot(y='Total',data=mart, width=0.2)

# **4. Create a basic BOX plot on one numeric variable by a


CATEGORICAL variable**
# In[30]:

sns.boxplot(x='Payment',y='Total',data=mart)

# **5. Create a basic BOX plot on one numeric variable by TWO


CATEGORICAL variable using HUE attribute**

# In[31]:

sns.boxplot(x='Payment',y='Total',hue='Gender',data=mart)

# **6. Add MEAN marker in the box plot using Showmeans attribute
and change its style using meanprops**

# In[34]:

sns.boxplot(x='Payment',y='Total',hue='Gender',data=mart,showmeans=True,meanprops={"mark
er":"o"
,"markerfacecolor":"white"
,"markersize":"10"
,"markeredgecolor":"black"})

# **7. Make HORIZONTAL box plot**


# In[35]:

sns.boxplot(x='Total',y='Payment',hue='Gender',data=mart,showmeans=True,meanprops={"mark
er":"o"
,"markerfacecolor":"white"
,"markersize":"5"
,"markeredgecolor":"black"})

# **8. Change PALETTE, LINE WIDTH etc..**

# In[41]:

sns.boxplot(x='Total',y='Payment',hue='Gender',data=mart,showmeans=True,meanprops={"mark
er":"o"
,"markerfacecolor":"white"
,"markersize":"5"
,"markeredgecolor":"black"}
,palette='CMRmap'
,linewidth=5)

# **9. Create box plot for EACH OF THE NUMERIC VARIABLE in


the dataframe**

# In[43]:
plt.figure(figsize=(15,5))

sns.boxplot(data=mart)

5.Seaborn_ViolinPlot

#!/usr/bin/env python
# coding: utf-8

# **1. Import all the required libraries**

# In[61]:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

# **2. Import the supermart data excel file**

# In[74]:
mart = pd.read_excel("D:/Learnerea/Tables/supermarket_sales.xlsx")
mart.head()

# **3. Create a basic VIOLINPLOT**

# In[76]:

sns.violinplot(y="Total",data=mart)

# **4. Create a VIOLINPLOT on two categorical and one numeric


variable and use split**

# In[79]:

sns.violinplot(x='Payment',y='Total',hue='Gender',data=mart,split=True)

# **5. Change the box in the VIOLINPLOT to horzontal lines**

# In[81]:

sns.violinplot(x='Payment',y='Total',hue='Gender',data=mart,split=True,inner='quartile')
# **6. Draw line for each observations in a VIOLINPLOT**

# In[82]:

sns.violinplot(x='Payment',y='Total',hue='Gender',data=mart,split=True,inner='stick')

# **7. Change the amount of smoothing using bw attribute**

# In[84]:

sns.violinplot(x='Payment',y='Total',hue='Gender',data=mart,split=True,inner='quartile',bw=.2)

# **8. Cut out the extreme values**

# In[85]:

sns.violinplot(x='Payment',y='Total',hue='Gender',data=mart,split=True,inner='quartile',bw=.2,cu
t=0)

6.Seaborn_StripPlot
#!/usr/bin/env python
# coding: utf-8

# **1. Import all the required libraries**

# In[61]:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

# **2. Import the supermart data excel file**

# In[74]:

mart = pd.read_excel("D:/Learnerea/Tables/supermarket_sales.xlsx")
mart.head()

# **3. Create a basic STRIP PLOT**


# In[160]:

sns.stripplot(data=mart, y='Total')

# **4. Expand markers in STRIP PLOT using jitter**

# In[138]:

sns.stripplot(data=mart, y='Total',jitter=0.2)

# **5. Draw line around the points using LINEWIDTH**

# In[141]:

sns.stripplot(data=mart, y='Total',linewidth=0.2)

# **6. Include third categorical variable with HUE**

# In[145]:
sns.stripplot(data=mart, y='Total',x='Payment',hue='Gender',jitter=0.2,linewidth=0.5)

# **7. Separate each level of hue using DODGE**

# In[146]:

sns.stripplot(data=mart,
y='Total',x='Payment',hue='Gender',jitter=0.2,linewidth=0.5,dodge=True)

# **8. Draw the strips on top of VIOLIN PLOT**

# In[157]:

sns.stripplot(data=mart, y='Total',x='Payment',jitter=0.1)
sns.violinplot(data=mart, y='Total',x='Payment',color='gray')

# **9. Draw the strips on top of BOX PLOT**

# In[158]:

sns.stripplot(data=mart, y='Total',x='Payment',color="black")
sns.boxplot(
data=mart, y='Total',x='Payment',color='gray')

7.Seaborn_SwarmPlot

#!/usr/bin/env python
# coding: utf-8

# **1. Import all the required libraries**

# In[1]:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

# **2. Import the supermarket sales data excel file**

# In[37]:

mart = pd.read_excel(r'D:\Learnerea\Tables\supermarket_sales.xlsx')
mart.head()

# **3. Create a basic SWARM PLOT on Total column**

# In[38]:

sns.swarmplot(data=mart,y='Total')

# **4. Create SWARM PLOT group by CATEGORIES**

# In[40]:
sns.swarmplot(data=mart,y='Total',x='Payment',hue='Gender')

# **5. Showing Hues Separatly on the categorical axis**

# In[63]:

plt.figure(figsize=(15,5))
sns.swarmplot(data=mart,y='Total',x='Payment',hue='Gender',split=True)

# **6. Styling a SWARM - Change the marker, size, color, edge


color.....**

# In[55]:

plt.figure(figsize=(15,5))
sns.swarmplot(data=mart,y='Total',x='Payment',size=10,color='green',edgecolor='black')

# **7. Overlay a SWARM PLOT on a BOX PLOT**

# In[57]:

sns.boxplot(data=mart,y='Total',x='Payment')
sns.swarmplot(data=mart,y='Total',x='Payment',color='black')

# **8. Overlay a SWARM PLOT on a VIOLIN PLOT**

# In[62]:

sns.violinplot(data=mart,y='Total',x='Payment',inner=None,width=1)
sns.swarmplot(data=mart,y='Total',x='Payment',color='white')
8.Seaborn_CatPlot

#!/usr/bin/env python
# coding: utf-8

# In[1]:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

# In[2]:

mart = pd.read_excel("D:/Learnerea/Tables/supermarket_sales.xlsx")
mart.head()

# In[57]:
p=sns.catplot(data=mart,x='Payment',y='Total',col='Gender',kind='box',row='Customer
type',palette='CMRmap_r')
# p.set_titles(col_template="{col_name}")

9.Seaborn_HistPlot

#!/usr/bin/env python
# coding: utf-8

# In[1]:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import statistics as sts

pd.set_option('display.max_rows',None)

# In[2]:

mart = pd.read_excel(r"D:\Learnerea\Tables\supermarket_sales.xlsx")
mart = mart[['Gender','Payment','Unit price','Quantity','Total','gross income']]
mart.head(10)

# **1. Create a basic HISTOGRAM on X or Y axis**

# In[26]:

sns.histplot(data=mart,y='Total')

# **2. Change the Bins Width using binwidth arguement**

# In[29]:

sns.histplot(data=mart,x='Total',binwidth=150)

# **3. Change Number of Bins and Interval**

# In[35]:

plt.figure(figsize=(15,5))
sns.histplot(data=mart,x='Total',bins=np.arange(0,1100,50))
plt.xticks(np.arange(0,1100,50))
# **4. Combine with KDE**

# In[38]:

sns.histplot(data=mart,x='Total',kde=True)

# **5. Use a categorical variable in HUE and STACK it using


MULTIPLE agruement**

# In[40]:

sns.histplot(data=mart,x='Total',hue='Payment',multiple='stack')

# **6. Make it a step/poly plot using ELEMENT arguement and change


the FILL**

# In[45]:

sns.histplot(data=mart,x='Total',hue='Payment',element='poly')

# **7. Use categorical variable and shrink it**


# In[50]:

sns.histplot(data=mart,x='Total',stat='probability')

# **8. Create a Bivariate Histogram**

# In[51]:

sns.histplot(data=mart,x='Total',y='gross income')

10.Seaborn_KDEplot

#!/usr/bin/env python
# coding: utf-8

# In[68]:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import statistics as sts

pd.set_option('display.max_rows',None)

# In[69]:

mart = pd.read_excel(r"D:\Learnerea\Tables\supermarket_sales.xlsx")
mart = mart[['Gender','Payment','Unit price','Quantity','Total','gross income']]
mart.head(10)

# **1. Create a basic KDE plot for Total column**

# In[70]:

sns.kdeplot(data=mart, x='Total')

# **2. Create KDE for all the numeric variables in dataframe**

# In[71]:
sns.kdeplot(data=mart)

# **3. Adjust the smooting using bw_adjust**

# In[74]:

sns.kdeplot(data=mart,x='Total',bw_adjust=0.5)

# **4. Group the KDE on a category variable**

# In[75]:

sns.kdeplot(data=mart,x='Total',hue='Payment')

# **5. Stack KDE on a category using MULTIPLE arguement**

# In[76]:

sns.kdeplot(data=mart,x='Total',hue='Payment',multiple='stack')
# **6. Use log scaling to map the variable in KDE**

# In[78]:

sns.kdeplot(data=mart,x='Total',log_scale=True)

# **7. Change styling of hued KDE using linewidth, palette, alpha


etc....**

# In[84]:

sns.kdeplot(data=mart,x='Total',hue='Payment',multiple='stack',linewidth=5,palette='Dark2',alph
a=0.5)

# **8. Creat a bivariate KDE**

# In[85]:

sns.kdeplot(data=mart, x='Unit price',y='gross income')

# **9. Group the bivariate KDE on a categorical variable and show the
contours**
# In[90]:

sns.kdeplot(data=mart, x='Unit price',y='gross


income',hue='Gender',fill=True,levels=5,thresh=0.2)

11.Seaborn_RugPlot

#!/usr/bin/env python
# coding: utf-8

# In[92]:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import statistics as sts

pd.set_option('display.max_rows',None)
# In[93]:

mart = pd.read_excel(r"D:\Learnerea\Tables\supermarket_sales.xlsx")
mart = mart[['Gender','Payment','Unit price','Quantity','Total','gross income']]
mart.head(10)

# **1. Create a basic RUG plot for one variable**

# In[152]:

plt.figure(figsize=(15,5))
sns.rugplot(data=mart, x='Unit price')

# **2. Create a RUG plot for two variables**

# In[153]:

plt.figure(figsize=(15,5))
sns.rugplot(data=mart, x='Unit price',y='gross income')

# **3. Group it by a categorical variable using hue**


# In[154]:

plt.figure(figsize=(15,5))
sns.rugplot(data=mart, x='Unit price',y='gross income',hue='Gender')

# **4. Change the height of Rugs in the RUG Plot**

# In[155]:

plt.figure(figsize=(15,5))
sns.rugplot(data=mart, x='Unit price',y='gross income',hue='Gender',height=0.05)

# **5. Combine this with a KDE Plot**

# In[157]:

plt.figure(figsize=(15,5))
sns.kdeplot(data=mart, x='Unit price',y='gross income',hue='Gender',fill=True)
sns.rugplot(data=mart, x='Unit price',y='gross income',hue='Gender')

# **6. Combine this with a SCATTER PLOT**


# In[159]:

# plt.figure(figsize=(15,5))
sns.scatterplot(data=mart, x='Unit price',y='gross income',hue='Gender')
sns.rugplot(data=mart, x='Unit price',y='gross income',hue='Gender')

# **7. Show Rugs outside of the Axis**

# In[165]:

plt.figure(figsize=(15,5))
sns.kdeplot(data=mart, x='Unit price',y='gross income',hue='Gender',fill=True)
sns.rugplot(data=mart, x='Unit price',y='gross income',hue='Gender',height=-0.01,clip_on=False)
12.Seaborn_ECDFplot

#!/usr/bin/env python
# coding: utf-8

# In[182]:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import statistics as sts

pd.set_option('display.max_rows',None)

# In[212]:

mart = pd.read_excel(r"D:\Learnerea\Tables\supermarket_sales.xlsx")
mart = mart[['Gender','Payment','Unit price','Quantity','Total','gross income']]
mart.head()

# In[219]:
sns.ecdfplot(data=mart,y='gross income',hue='Gender',stat='count')

13.Seaorn_DisPlot

#!/usr/bin/env python
# coding: utf-8

# In[2]:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

# In[57]:

mart = pd.read_excel(r"D:\Learnerea\Tables\supermarket_sales.xlsx")
mart = mart[['Gender','Payment','Unit price','Quantity','Total','gross income','Branch']]
mart.head()

# In[60]:

sns.displot(data=mart,y='Total',kde=True,rug=True,rug_kws={'height':0.05},hue='Payment',mult
iple='stack',col='Branch',row='Gender')

# In[62]:

sns.displot(data=mart,y='Total',kind='ecdf',hue='Payment',col='Branch',row='Gender')
14.Seaorn_JointPlot

#!/usr/bin/env python
# coding: utf-8

# In[264]:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

pd.set_option("display.max_columns",None)

# In[265]:

iris = sns.load_dataset('iris')
iris.head()

# **1. Drawing a first basic JOINT Plot**

# In[266]:

sns.jointplot(data=iris,x='sepal_length',y='petal_length')

# **2. Change its kind to “scatter”|“kde”|“hist”|“hex”|“reg”|“resid”**

# In[272]:
sns.jointplot(data=iris,x='sepal_length',y='petal_length'
,kind='resid')

# **3. Grouping based on a categorical variable**

# In[274]:

sns.jointplot(data=iris,x='sepal_length',y='petal_length'
,hue='species')

# **4. Formatting All and Individual Plots**

# In[285]:

sns.jointplot(data=iris,x='sepal_length',y='petal_length'
# ,color='red'
# ,hue='species'
# ,palette='BuPu'
,joint_kws=dict(marker='+'
,color='red')
,marginal_kws=dict(color='green',kde=True))

# **5. Adjust height, ratio, space and show/hide the marginal_ticks**


# In[299]:

sns.jointplot(data=iris,x='sepal_length',y='petal_length'
,height=5
,ratio=3
# ,space=5
,marginal_ticks=True)

# **6. Plot KDE and RUG on top of Joint Plot**

# In[308]:

l=sns.jointplot(data=iris,x='sepal_length',y='petal_length',joint_kws=dict(color='red'))
l.plot_joint(sns.kdeplot)
l.plot_joint(sns.rugplot,height=0.1,color='red')
15.Seaorn_PairPlot

#!/usr/bin/env python
# coding: utf-8

# In[9]:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

# In[65]:

iris = sns.load_dataset('iris')
# iris = iris[['species','sepal_length','petal_length']]
iris.head()
# **1. Creating a basic PAIR PLOT**

# In[66]:

sns.pairplot(data=iris)

# **2. Changing the diagonal plot KIND to KDE, HIST or None**

# In[67]:

sns.pairplot(data=iris,diag_kind='kde')

# **3. Changing the non diagonal plot KIND to scatter, kde, hist or
reg**

# In[68]:

sns.pairplot(data=iris,kind="reg")
# **4. Adding hue to the PAIR PLOT**

# In[69]:

sns.pairplot(data=iris,hue='species')

# **5. Creating PAIR PLOT for specific list of variables


(diag_kind=None)**

# In[72]:

sns.pairplot(data=iris,x_vars=['sepal_width','petal_width','petal_length'],y_vars=['sepal_length','s
epal_width'],diag_kind=None)

# **6. Showing only the lower triangle using CORNOR arguement**

# In[74]:

sns.pairplot(data=iris,corner=True)

# **7. Making changes specific to Diagonal and Non-Diagonal plots


separatly**
# In[81]:

sns.pairplot(data=iris,diag_kws=dict(color='green',kde=True)
,plot_kws=dict(color='red',marker=10,s=100))

# **8. Creating KDE plot on top of the PAIR PLOT using map_lower or
upper**

# In[87]:

l=sns.pairplot(data=iris,plot_kws=dict(color='red'))
l.map_upper(sns.kdeplot)
l.map_lower(sns.kdeplot)

16.Seaborn_ScatterPlot
#!/usr/bin/env python
# coding: utf-8

# In[195]:

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

# In[239]:

mart = pd.read_csv(r'D:\Learnerea\Tables\train.csv').sample(50)
mart[['Tier']]=mart['Outlet_Location_Type'].str.split(expand=True)[1]
mart.to_excel(r'D:\Learnerea\Tables\mart_train_sample.xlsx',index=False)

# **1. Create a basic SCATTER PLOT**

# In[216]:

sns.scatterplot(data=mart,x='Item_MRP',y='Sales')

# **2. Grouping basis on a categorical variable using HUE**


# In[217]:

sns.scatterplot(data=mart,x='Item_MRP',y='Sales',hue='Outlet_Size')

# **3. Grouping basis on a categorical variable using STYLE and


Styling Markers as well**

# In[219]:

sns.scatterplot(data=mart,x='Item_MRP',y='Sales',style='Outlet_Size'
,markers={"High":"^","Small":"v","Medium":"o"})

# **4. Grouping basis on a categorical variable using HUE & STYLE


both together**

# In[237]:

sns.scatterplot(data=mart,x='Item_MRP',y='Sales',style='Outlet_Size'
,hue='Outlet_Size'
,s=200
,markers={"High":"^","Small":"v","Medium":"o"})
# **5. Grouping basis on numeric variable using HUE and use
PALETTE**

# In[226]:

sns.scatterplot(data=mart,x='Item_MRP',y='Sales'
# ,style='Outlet_Size'
,hue='Outlet_Size'
,palette='Purples_r'
,markers={"High":"^","Small":"v","Medium":"o"})

# **6. Grouping basis on a numeric variable using HUE & SIZE**

# In[231]:

sns.scatterplot(data=mart,x='Item_MRP',y='Sales'
,hue='Outlet_Size'
,size='Tier'
,sizes=(20,200)
,legend='full'
,palette='Purples_r'
,markers={"High":"^","Small":"v","Medium":"o"})
# **7. Change the Marker size with S arguement**

# In[236]:

sns.scatterplot(data=mart,x='Item_MRP',y='Sales',s=500,color='red',edgecolor='black')

17.Seaborn_LinePlot

#!/usr/bin/env python
# coding: utf-8

# In[1]:

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
# In[4]:

mart = pd.read_csv(r'D:\Learnerea\Tables\train.csv')
mart[['Tier']]=mart['Outlet_Location_Type'].str.split(expand=True)[1]
mart.head()

# **1. Creat a basic line plot and try different estimators**

# In[336]:

#---Estimator = None"
# height = [5, 5, 7, 5, 8]
# age = [22,42,30,35,40]

# sns.lineplot(x=age,y=height,estimator=None)

#---Estimator = sum
height = [5, 6, 7, 6.5,4]
age = [22,25,30,25,25]
sns.lineplot(x=age,y=height,estimator=sum)

# In[334]:
sns.lineplot(data=mart,x="Outlet_Year",y="Sales",ci=None,estimator=None)

# **2. Test with different Confidence Intervals and with different


number of Bootstraping**

# In[342]:

sns.lineplot(data=mart,x="Outlet_Year",y="Sales",n_boot=2)

# **3. Grouping using HUE and use different Palettes**

# In[347]:

sns.lineplot(data=mart,x="Outlet_Year",y="Sales",ci=None
,hue='Outlet_Size'
,palette=['red','green','blue'])

# **4. Grouping using Style, use different Marker and Dashes**

# In[350]:

sns.lineplot(data=mart,x="Outlet_Year",y="Sales",ci=None
,style='Outlet_Size'
,markers=True
,dashes=False)

# **5. Grouping using Size**

# In[353]:

sns.lineplot(data=mart,x="Outlet_Year",y="Sales",ci=None
,size='Outlet_Size'
,sizes=(.5,5))

# **6. Use different semantic styling parameters on same or different


variables**

# In[360]:

sns.lineplot(data=mart,x="Outlet_Year",y="Sales",ci=None
,hue='Outlet_Size'
# ,style='Outlet_Size'
# ,markers=True
,size='Tier'
)
18.Seaborn_RelPlot

#!/usr/bin/env python
# coding: utf-8

# In[60]:

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

# In[61]:

mart = pd.read_excel(r'D:\Learnerea\Tables\mart_linePlot.xlsx').sample(n=50)
mart.columns=mart.columns.str.lower()
mart.head()
# In[76]:

mart.to_excel(r'D:\Learnerea\Tables\mart_relPlot.xlsx',index=False)

# In[75]:

sns.relplot(data=mart,x='outlet_year',y='sales',s=100,hue='outlet_size'
# ,col='outlet_size'
# ,row='tier'
# ,hue='outlet_size'
,palette=['green','red','yellow'])

19.Seaborn_RegPlot
#!/usr/bin/env python
# coding: utf-8

# In[2]:

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

# In[3]:

tips = sns.load_dataset('tips')
tips.head()

# **1. Creat a basic reg plot**

# In[23]:

sns.regplot(data=tips,x='total_bill',y='tip')
# **2. Change the styling of reg plot, only line, only scatter, show/hide
the ci, change the ci value, change n_boot**

# In[38]:

sns.regplot(data=tips,x='total_bill',y='tip'
# ,color='red'
# ,marker='+'
,line_kws=dict(color='red',linestyle='--')
,scatter_kws=dict(s=100,color='green',alpha=0.5)
,ci=60
,n_boot=500)

# **3. Discuss aobut the statistical models in seaborn**

# In[39]:

sns.regplot(data=tips,x='total_bill',y='tip')

20.Seaborn_HeatMap

#!/usr/bin/env python
# coding: utf-8

# In[58]:

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns

# In[12]:

mart = pd.read_excel(r'D:\Learnerea\Tables\mart_linePlot.xlsx')
mart.columns = mart.columns.str.lower()
mart.head(10)

# In[61]:

martPiv = mart.pivot_table(index='outlet_year',columns='outlet_size',values='sales')
martPiv

# In[66]:

sns.set_style('white')
plt.figure(figsize=(5,5))

sns.heatmap(martPiv
# ,square=True
,annot=True
,fmt='.0f'
,annot_kws=dict(size=15,weight='bold')
,linewidths=0.5
,linecolor='black'
,cmap='OrRd_r')

# In[68]:

sns.heatmap(mart.corr()
,vmin=-1
,vmax=1
,center=0
,cmap='OrRd_r'
,annot=True
,fmt='.1f'
,annot_kws=dict(size=15,weight='bold')
,linecolor='black'
,linewidths=0.5)
21.Seaborn_ClusterMap

#!/usr/bin/env python
# coding: utf-8

# In[45]:

import pandas as pd
import matplotlib.pyplot as plt
# import numpy as np
import seaborn as sns
# from sunbird.categorical_encoding import frequency_encoding

pd.set_option('display.max_rows',None)

# In[87]:

mart = pd.read_excel(r'D:\Learnerea\Tables\mart_linePlot.xlsx')
mart.columns = mart.columns.str.lower()
mart.head(10)

# In[88]:

martPiv=mart.pivot_table(index='outlet_year',columns='outlet_size',values='sales')
martPiv.head()

# In[111]:

sns.clustermap(martPiv
# ,col_cluster=False
,row_cluster=False
,annot=True
,fmt='.0f'
# ,z_score=1
,standard_scale=1
,linewidth=.5
)

22.Seaborn_FacetGrid

#!/usr/bin/env python
# coding: utf-8

# In[95]:

import pandas as pd
import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt

# In[135]:
mart = pd.read_excel(r'D:\Learnerea\Tables\mart_linePlot.xlsx')
mart.columns = mart.columns.str.lower()
mart.head()

# In[125]:

mart.outlet_size.unique()

# In[136]:

mart.outlet_location_type.unique()

# In[142]:

learnerea=sns.FacetGrid(mart,col='outlet_size')
learnerea.map_dataframe(sns.scatterplot,x='outlet_year',y='sales',hue='outlet_location_type'
,marker='+'
,alpha=0.5
,color='red'
,s=100)

# In[152]:
learnerea=sns.FacetGrid(mart,col='outlet_size',row='outlet_location_type',sharey=False,ylim=(0,
1000))
learnerea.map_dataframe(sns.histplot,x='sales')
learnerea.set_axis_labels('sales','count')
learnerea.set_titles(col_template='{col_name} Size',row_template='{row_name} Type')

You might also like