Update seaborn-plotting.md

pull/593/head
Ashita Prasad 2024-06-22 20:10:13 +05:30 zatwierdzone przez GitHub
rodzic 8174be5b37
commit 469e6c4d35
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: B5690EEEBB952194
1 zmienionych plików z 93 dodań i 72 usunięć

Wyświetl plik

@ -1,12 +1,16 @@
### Seaborn Plotting Functions: # Seaborn
Seaborn is a powerful and easy-to-use data visualization library in Python built on top of Matplotlib.It provides a high-level interface for drawing attractive and informative statistical graphics.Now we will cover various functions covered by Seaborn, along with examples to illustrate their usage. Seaborn is a powerful and easy-to-use data visualization library in Python built on top of Matplotlib.It provides a high-level interface for drawing attractive and informative statistical graphics.Now we will cover various functions covered by Seaborn, along with examples to illustrate their usage.
Seaborn simplifies the process of creating complex visualizations with a few lines of code and it integrates closely with pandas data structure , making it an excellent choice for data analysis and exploration. Seaborn simplifies the process of creating complex visualizations with a few lines of code and it integrates closely with pandas data structure , making it an excellent choice for data analysis and exploration.
### Setting up Seaborn:
## Setting up Seaborn
Make sure seaborn library is installed in your system. If not use command Make sure seaborn library is installed in your system. If not use command
`pip install seaborn` `pip install seaborn`
After installing you are all set to experiment with plotting functions. After installing you are all set to experiment with plotting functions.
``` ```python
#import necessary libraries #import necessary libraries
import seaborn as sns import seaborn as sns
@ -16,24 +20,27 @@ import pandas as pd
Seaborn includes several built-in datasets that you can use for practice Seaborn includes several built-in datasets that you can use for practice
You can list all available datasets using below command You can list all available datasets using below command
`sns.get_dataset_names()` ```python
sns.get_dataset_names()
```
Here we are using 'tips' dataset Here we are using 'tips' dataset
``` ```python
# loading an example dataset # loading an example dataset
tips=sns.load_dataset('tips') tips=sns.load_dataset('tips')
``` ```
Before delving into plotting, make yourself comfortable with the dataset. To do that, use the pandas library to understand what information the dataset contains and preprocess the data. If you get stuck, feel free to refer to the pandas documentation. Before delving into plotting, make yourself comfortable with the dataset. To do that, use the pandas library to understand what information the dataset contains and preprocess the data. If you get stuck, feel free to refer to the pandas documentation.
### Relational Plots: ## Relational Plots
Relational plots are used to visualize the relationship between two or more variables
##### Scatter Plot:
A scatter plot displays data points based on two numerical variables.Seaborn `scatterplot` function allows you to create scatter plots with ease
##### Line Plot:
A line plot connects data points in the order they appear in the dataset.This is useful for time series data.`lineplot` function allows you to create lineplots.
``` Relational plots are used to visualize the relationship between two or more variables
### Scatter Plot
A scatter plot displays data points based on two numerical variables.Seaborn `scatterplot` function allows you to create scatter plots with ease
```python
# scatter plot using Seaborn # scatter plot using Seaborn
plt.figure(figsize=(5,5)) plt.figure(figsize=(5,5))
@ -41,9 +48,12 @@ sns.scatterplot(data=tips,x='total_bill',y='tip',hue='day',style='time')
plt.title('Scatter Plot of Total Bill vs Tip') plt.title('Scatter Plot of Total Bill vs Tip')
plt.show() plt.show()
``` ```
![Alt text](/images/plot_images/image1.png) ![scatter plot](images/seaborn-plotting/image1.png)
``` ### Line Plot
A line plot connects data points in the order they appear in the dataset.This is useful for time series data.`lineplot` function allows you to create lineplots.
```python
# lineplot using seaborn # lineplot using seaborn
plt.figure(figsize=(5,5)) plt.figure(figsize=(5,5))
@ -51,20 +61,17 @@ sns.lineplot(data=tips,x='size',y='total_bill',hue='day')
plt.title('Line Plot of Total Bill by Size and Day') plt.title('Line Plot of Total Bill by Size and Day')
plt.show() plt.show()
``` ```
![Alt text](images/plot_images/image2.png)
### Distribution Plots: ![lineplot](images/seaborn-plotting/image2.png)
## Distribution Plots
Distribution Plots visualize the distribution of a single numerical variable Distribution Plots visualize the distribution of a single numerical variable
##### HistPlot:
A histplot displays the distribution of a numerical variable by dividing the data into bins.
##### KDE Plot:
A Kernel Density Estimate (KDE) plot represents the distribution of a variable as a smooth curve.
##### ECDF Plot:
An Empirical Cumulative Distribution Function (ECDF) plot shows the proportion of data points below each value.
##### Rug Plot:
A rug plot in Seaborn is a simple way to show the distribution of a variable by drawing small vertical lines (or "rugs") at each data point along the x-axis.
``` ### HistPlot
A histplot displays the distribution of a numerical variable by dividing the data into bins.
```python
# Histplot using Seaborn # Histplot using Seaborn
plt.figure(figsize=(5,5)) plt.figure(figsize=(5,5))
@ -72,9 +79,12 @@ sns.histplot(data=tips, x='total_bill', kde=True)
plt.title('Histplot of Total Bill') plt.title('Histplot of Total Bill')
plt.show() plt.show()
``` ```
![Alt text](/images/plot_images/image3.png) ![Histplot](images/seaborn-plotting/image3.png)
``` ### KDE Plot
A Kernel Density Estimate (KDE) plot represents the distribution of a variable as a smooth curve.
```python
# KDE Plot using Seaborn # KDE Plot using Seaborn
plt.figure(figsize=(5,5)) plt.figure(figsize=(5,5))
@ -82,9 +92,12 @@ sns.kdeplot(data=tips, x='total_bill', hue='sex', fill=True)
plt.title('KDE Plot of Total Bill by Sex') plt.title('KDE Plot of Total Bill by Sex')
plt.show() plt.show()
``` ```
![Alt text](/images/plot_images/image4.png) ![KDE](images/seaborn-plotting/image4.png)
``` ### ECDF Plot
An Empirical Cumulative Distribution Function (ECDF) plot shows the proportion of data points below each value.
```python
# ECDF Plot using Seaborn # ECDF Plot using Seaborn
plt.figure(figsize=(5,5)) plt.figure(figsize=(5,5))
@ -92,9 +105,12 @@ sns.ecdfplot(data=tips, x='total_bill', hue='sex')
plt.title('ECDF Plot of Total Bill by Sex') plt.title('ECDF Plot of Total Bill by Sex')
plt.show() plt.show()
``` ```
![Alt text](/images/plot_images/image5.png) ![ECDF](images/seaborn-plotting/image5.png)
``` ### Rug Plot
A rug plot in Seaborn is a simple way to show the distribution of a variable by drawing small vertical lines (or "rugs") at each data point along the x-axis.
```python
# Rug Plot using Seaborn # Rug Plot using Seaborn
plt.figure(figsize=(3,3)) plt.figure(figsize=(3,3))
@ -102,20 +118,15 @@ sns.rugplot(x='total_bill', data=tips)
plt.title('Rug Plot of Total Bill Amounts') plt.title('Rug Plot of Total Bill Amounts')
plt.show() plt.show()
``` ```
![Alt text](/images/plot_images/image6.png) ![Rug](images/seaborn-plotting/image6.png)
### Categorical Plots: ## Categorical Plots
Categorical plots are used to visualize data where one or more variables are categorical. Categorical plots are used to visualize data where one or more variables are categorical.
##### Bar Plot:
A bar plot shows the relationship between a categorical variable and a numerical variable.
##### Point Plot:
A point plot in Seaborn is used to show the relationship between two categorical variables, with the size of the points representing the values of third variable.
##### Box Plot:
A box plot displays the distribution of a numerical variable across different categories.
##### Violin Plot:
A violin plot combines aspects of a box plot and a KDE plot to show the distribution of data
``` ### Bar Plot
A bar plot shows the relationship between a categorical variable and a numerical variable.
```python
# Bar Plot using Seaborn # Bar Plot using Seaborn
plt.figure(figsize=(5,5)) plt.figure(figsize=(5,5))
@ -123,9 +134,12 @@ sns.barplot(data=tips,x='day',y='total_bill',hue='sex')
plt.title('Bar Plot of Total Bill by Day and Sex') plt.title('Bar Plot of Total Bill by Day and Sex')
plt.show() plt.show()
``` ```
![Alt text](/images/plot_images/image7.png) ![Bar](images/seaborn-plotting/image7.png)
``` ### Point Plot
A point plot in Seaborn is used to show the relationship between two categorical variables, with the size of the points representing the values of third variable.
```python
# Point Plot using Seaborn # Point Plot using Seaborn
plt.figure(figsize=(5,5)) plt.figure(figsize=(5,5))
@ -133,9 +147,12 @@ sns.pointplot(x='day',y='total_bill',hue='sex',data=tips)
plt.title('Average Total Bill by Day and Sex') plt.title('Average Total Bill by Day and Sex')
plt.show() plt.show()
``` ```
![Alt text](/images/plot_images/image8.png) ![Point](images/seaborn-plotting/image8.png)
``` ### Box Plot
A box plot displays the distribution of a numerical variable across different categories.
```python
# Box Plot using Seaborn # Box Plot using Seaborn
plt.figure(figsize=(5,5)) plt.figure(figsize=(5,5))
@ -143,9 +160,12 @@ sns.boxplot(data=tips, x='day', y='total_bill', hue='sex')
plt.title('Box Plot of Total Bill by Day and Sex') plt.title('Box Plot of Total Bill by Day and Sex')
plt.show() plt.show()
``` ```
![Alt text](/images/plot_images/image9.png) ![Box](images/seaborn-plotting/image9.png)
``` ### Violin Plot
A violin plot combines aspects of a box plot and a KDE plot to show the distribution of data
```python
# Violin Plot using Seaborn # Violin Plot using Seaborn
plt.figure(figsize=(5,5)) plt.figure(figsize=(5,5))
@ -153,14 +173,15 @@ sns.violinplot(data=tips,x='day',y='total_bill',hue='sex',split=True)
plt.title('Violin Plot of Total Bill by Day and Sex') plt.title('Violin Plot of Total Bill by Day and Sex')
plt.show() plt.show()
``` ```
![Alt text](/images/plot_images/image10.png) ![Violin](images/seaborn-plotting/image10.png)
### Matrix Plots: ## Matrix Plots
Matrix plots are useful for visualizing data in a matrix format. Matrix plots are useful for visualizing data in a matrix format.
##### Heatmap:
### Heatmap
A heatmap displays data in a matrix where values are represented by color. A heatmap displays data in a matrix where values are represented by color.
``` ```python
# Heatmap using Seaborn # Heatmap using Seaborn
plt.figure(figsize=(10,8)) plt.figure(figsize=(10,8))
@ -170,12 +191,12 @@ sns.heatmap(flights_pivot, annot=True, fmt='d', cmap='YlGnBu')
plt.title('Heatmap of Flight Passengers') plt.title('Heatmap of Flight Passengers')
plt.show() plt.show()
``` ```
![Alt text](/images/plot_images/image11.png) ![Heatmap](images/seaborn-plotting/image11.png)
### Pair Plot: ## Pair Plot
A pair plot shows the pairwise relationships between multiple variables in a dataset. A pair plot shows the pairwise relationships between multiple variables in a dataset.
``` ```python
#Pairplot using Seaborn #Pairplot using Seaborn
plt.figure(figsize=(5,5)) plt.figure(figsize=(5,5))
@ -183,12 +204,12 @@ sns.pairplot(tips, hue='sex')
plt.title('Pair Plot of Tips Dataset') plt.title('Pair Plot of Tips Dataset')
plt.show() plt.show()
``` ```
![Alt text](/images/plot_images/image12.png) ![Pair](images/seaborn-plotting/image12.png)
### FacetGrid: ## FacetGrid
FacetGrid allows you to create a grid of plots based on the values of one or more categorical variables. FacetGrid allows you to create a grid of plots based on the values of one or more categorical variables.
``` ```python
#Facetgrid using Seaborn #Facetgrid using Seaborn
plt.figure(figsize=(5,5)) plt.figure(figsize=(5,5))
@ -196,43 +217,43 @@ g = sns.FacetGrid(tips, col='sex', row='time', margin_titles=True)
g.map(sns.scatterplot, 'total_bill', 'tip') g.map(sns.scatterplot, 'total_bill', 'tip')
plt.show() plt.show()
``` ```
![Alt text](/images/plot_images/image13.png) ![Facetgrid](images/seaborn-plotting/image13.png)
### Customizing Seaborn Plots: ## Customizing Seaborn Plots
Seaborn plots can be customized to improve their appearance and convey more information. Seaborn plots can be customized to improve their appearance and convey more information.
##### Changing the Aesthetic Style: ### Changing the Aesthetic Style
Seaborn comes with several built-in themes. Seaborn comes with several built-in themes.
``` ```python
sns.set_style('whitegrid') sns.set_style('whitegrid')
sns.scatterplot(data=tips, x='total_bill', y='tip') sns.scatterplot(data=tips, x='total_bill', y='tip')
plt.title('Scatter Plot with Whitegrid Style') plt.title('Scatter Plot with Whitegrid Style')
plt.show() plt.show()
``` ```
![Alt text](/images/plot_images/image14.png) ![Aesthetic](images/seaborn-plotting/image14.png)
##### Customizing Colors: ### Customizing Colors
You can use color palettes to customize the colors in your plots. You can use color palettes to customize the colors in your plots.
``` ```python
sns.set_palette('pastel') sns.set_palette('pastel')
sns.barplot(data=tips, x='day', y='total_bill', hue='sex') sns.barplot(data=tips, x='day', y='total_bill', hue='sex')
plt.title('Bar Plot with Pastel Palette') plt.title('Bar Plot with Pastel Palette')
plt.show() plt.show()
``` ```
![Alt text](/images/plot_images/image15.png) ![Colors](images/seaborn-plotting/image15.png)
##### Adding Titles and Labels: ### Adding Titles and Labels
Titles and labels can be added to make plots more informative. Titles and labels can be added to make plots more informative.
``` ```python
plot = sns.scatterplot(data=tips, x='total_bill', y='tip') plot = sns.scatterplot(data=tips, x='total_bill', y='tip')
plot.set_title('Scatter Plot of Total Bill vs Tip') plot.set_title('Scatter Plot of Total Bill vs Tip')
plot.set_xlabel('Total Bill ($)') plot.set_xlabel('Total Bill ($)')
plot.set_ylabel('Tip ($)') plot.set_ylabel('Tip ($)')
plt.show() plt.show()
``` ```
![Alt text](/images/plot_images/image16.png) ![Titles](images/seaborn-plotting/image16.png)
Seaborn is a versatile library that simplifies the creation of complex visualizations. By using Seaborn's plotting functions, you can create a wide range of statistical graphics with minimal effort. Whether you're working with relational data, categorical data, or distributions, Seaborn provides the tools you need to visualize your data effectively. Seaborn is a versatile library that simplifies the creation of complex visualizations. By using Seaborn's plotting functions, you can create a wide range of statistical graphics with minimal effort. Whether you're working with relational data, categorical data, or distributions, Seaborn provides the tools you need to visualize your data effectively.