Seaborn
Seaborn
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')
python
Copy code
Seaborn 1
data.head()
python
Copy code
sns.scatterplot(x='total_bill', y='tip', data=data)
plt.show()
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()
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()
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()
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()
python
Copy code
sns.clustermap(corr, cmap='coolwarm')
Seaborn 4
plt.show()
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()
python
Copy code
g = sns.FacetGrid(data, col='time')
g.map(sns.histplot, 'total_bill')
plt.show()
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()
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()
python
Copy code
sns.scatterplot(x='total_bill', y='tip', hue='time', size
='size', data=data)
plt.show()
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.
Seaborn 8