0% found this document useful (0 votes)
119 views

Seaborn

A tutorial in seaborn

Uploaded by

hikmatbaniya20
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)
119 views

Seaborn

A tutorial in seaborn

Uploaded by

hikmatbaniya20
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
You are on page 1/ 8

Seaborn

1. Installation and Setup


First, install Seaborn if you haven't already:

bash
Copy code
pip install seaborn

Import Seaborn and other necessary libraries in your Python script or Jupyter
notebook:

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

css
Copy code
- Load a built-in dataset to practice:
```python
data = sns.load_dataset('tips')

Basic overview of the dataset:

python
Copy code

Seaborn 1
data.head()

2. Basic Plots in Seaborn


Scatter Plot: Use scatterplot to visualize the relationship between two
continuous variables.

python
Copy code
sns.scatterplot(x='total_bill', y='tip', data=data)
plt.show()

Line Plot: Use lineplot for continuous data points.

python
Copy code
sns.lineplot(x='size', y='total_bill', data=data)
plt.show()

Bar Plot: Use barplot to show the central tendency for a categorical variable.

python
Copy code
sns.barplot(x='day', y='total_bill', data=data)
plt.show()

Count Plot: Use countplot to show the count of observations in each


categorical bin.

python
Copy code

Seaborn 2
sns.countplot(x='day', data=data)
plt.show()

3. Distribution Plots
Histogram and KDE Plot: Use histplot for histograms and kdeplot for kernel
density estimation.

python
Copy code
sns.histplot(data['total_bill'], kde=True)
plt.show()

Box Plot: Use boxplot to show the distribution of data based on a five-number
summary.

python
Copy code
sns.boxplot(x='day', y='total_bill', data=data)
plt.show()

Violin Plot: Use violinplot to show the distribution of data across different
categories.

python
Copy code
sns.violinplot(x='day', y='total_bill', data=data)
plt.show()

4. Categorical Data Visualization

Seaborn 3
Swarm Plot: Use swarmplot for a categorical scatter plot with non-overlapping
data points.

python
Copy code
sns.swarmplot(x='day', y='total_bill', data=data)
plt.show()

Strip Plot: Similar to swarmplot but allows overlapping of data points.

python
Copy code
sns.stripplot(x='day', y='total_bill', data=data, jitter=T
rue)
plt.show()

5. Matrix Plots
Heatmap: Use heatmap to show data in a matrix form with color mapping.

python
Copy code
corr = data.corr()
sns.heatmap(corr, annot=True, cmap='coolwarm')
plt.show()

Clustermap: Use clustermap for clustering data into a heatmap.

python
Copy code
sns.clustermap(corr, cmap='coolwarm')

Seaborn 4
plt.show()

6. Pair Plots and Joint Plots


Pair Plot: Use pairplot to visualize the pairwise relationships in a dataset.

python
Copy code
sns.pairplot(data)
plt.show()

Joint Plot: Use jointplot to show a scatter plot and the distribution of each
variable.

python
Copy code
sns.jointplot(x='total_bill', y='tip', data=data, kind='sc
atter')
plt.show()

7. Facet Grids and Pair Grids


FacetGrid: Use FacetGrid to map a plot to multiple panels.

python
Copy code
g = sns.FacetGrid(data, col='time')
g.map(sns.histplot, 'total_bill')
plt.show()

PairGrid: Use PairGrid for customized pair plots.

Seaborn 5
python
Copy code
g = sns.PairGrid(data)
g.map_upper(sns.scatterplot)
g.map_lower(sns.kdeplot)
g.map_diag(sns.histplot)
plt.show()

8. Customizing Plots
Changing Aesthetics: Use Seaborn's built-in themes for styling.

python
Copy code
sns.set_theme(style='whitegrid')
sns.boxplot(x='day', y='total_bill', data=data)
plt.show()

Adding Titles and Labels:

python
Copy code
sns.boxplot(x='day', y='total_bill', data=data)
plt.title('Total Bill Distribution by Day')
plt.xlabel('Day')
plt.ylabel('Total Bill')
plt.show()

Palette Customization:

python
Copy code

Seaborn 6
sns.barplot(x='day', y='total_bill', data=data, palette='p
astel')
plt.show()

9. Advanced Features
Hue Parameter: Use hue to add another dimension to plots.

python
Copy code
sns.scatterplot(x='total_bill', y='tip', hue='time', data=
data)
plt.show()

Size and Style Parameters: Modify the appearance of plots further.

python
Copy code
sns.scatterplot(x='total_bill', y='tip', hue='time', size
='size', data=data)
plt.show()

10. Saving Plots


Save the plot to a file using plt.savefig .

python
Copy code
sns.boxplot(x='day', y='total_bill', data=data)
plt.savefig('boxplot.png')
plt.show()

Seaborn 7
11. Exercises and Practice
Practice with various datasets such as Iris, Titanic, and others available in
Seaborn.

Try to answer questions like:

How does the distribution of tips vary across days?

Is there a relationship between total bill and tip amount?

How do different categorical variables affect total bill?

12. Learning Resources


Documentation: Seaborn Documentation

Tutorials: Look for Seaborn tutorials on platforms like Kaggle, YouTube, or


Coursera

Seaborn 8

You might also like