structure updates
This commit is contained in:
114
tech_docs/python/Seaborn.md
Normal file
114
tech_docs/python/Seaborn.md
Normal file
@@ -0,0 +1,114 @@
|
||||
For data visualization in Python, `Seaborn` is a very useful library that builds on top of `Matplotlib`. It provides a high-level interface for drawing attractive and informative statistical graphics. Seaborn simplifies the process of creating complex visualizations from data in pandas DataFrames and arrays, integrating closely with the rest of the Python data science stack. Here's a concise reference guide for common use cases with `Seaborn`:
|
||||
|
||||
# `Seaborn` Reference Guide
|
||||
|
||||
## Installation
|
||||
```
|
||||
pip install seaborn
|
||||
```
|
||||
|
||||
## Basic Usage
|
||||
|
||||
### Importing Seaborn
|
||||
```python
|
||||
import seaborn as sns
|
||||
```
|
||||
|
||||
### Setting Aesthetics
|
||||
```python
|
||||
# Set the aesthetic style of the plots
|
||||
sns.set_style("whitegrid")
|
||||
|
||||
# Set the context for the plot (paper, notebook, talk, poster)
|
||||
sns.set_context("notebook")
|
||||
```
|
||||
|
||||
## Basic Plots
|
||||
|
||||
### Distribution Plots
|
||||
```python
|
||||
import numpy as np
|
||||
|
||||
# Load a dataset for example
|
||||
tips = sns.load_dataset("tips")
|
||||
|
||||
# Histogram
|
||||
sns.histplot(data=tips, x="total_bill")
|
||||
|
||||
# Kernel Density Estimate (KDE) plot
|
||||
sns.kdeplot(data=tips, x="total_bill")
|
||||
|
||||
# Combining histogram and KDE
|
||||
sns.histplot(data=tips, x="total_bill", kde=True)
|
||||
```
|
||||
|
||||
### Categorical Plots
|
||||
```python
|
||||
# Box plot
|
||||
sns.boxplot(x="day", y="total_bill", data=tips)
|
||||
|
||||
# Violin plot
|
||||
sns.violinplot(x="day", y="total_bill", data=tips)
|
||||
|
||||
# Swarm plot
|
||||
sns.swarmplot(x="day", y="total_bill", data=tips)
|
||||
```
|
||||
|
||||
### Scatter Plots
|
||||
```python
|
||||
# Scatter plot with linear regression model fit
|
||||
sns.regplot(x="total_bill", y="tip", data=tips)
|
||||
|
||||
# Scatter plot without regression model
|
||||
sns.scatterplot(x="total_bill", y="tip", data=tips)
|
||||
```
|
||||
|
||||
### Heatmaps
|
||||
```python
|
||||
# Compute the correlation matrix
|
||||
corr = tips.corr()
|
||||
|
||||
# Generate a heatmap
|
||||
sns.heatmap(corr, annot=True, fmt=".2f")
|
||||
```
|
||||
|
||||
### Pair Plots
|
||||
```python
|
||||
# Pairwise relationships in a dataset
|
||||
sns.pairplot(tips)
|
||||
```
|
||||
|
||||
## Advanced Visualizations
|
||||
|
||||
### Facet Grids
|
||||
```python
|
||||
# Create a facet grid
|
||||
g = sns.FacetGrid(tips, col="time", row="smoker")
|
||||
g.map(sns.scatterplot, "total_bill", "tip")
|
||||
```
|
||||
|
||||
### Joint Plots
|
||||
```python
|
||||
# Draw a plot of two variables with bivariate and univariate graphs
|
||||
sns.jointplot(x="total_bill", y="tip", data=tips, kind="hex")
|
||||
```
|
||||
|
||||
## Customizing Plots
|
||||
|
||||
### Control Figure Aesthetics
|
||||
```python
|
||||
# Customize the appearance
|
||||
sns.set(style="darkgrid", palette="pastel", font="Verdana", font_scale=1.1)
|
||||
```
|
||||
|
||||
### Saving Plots
|
||||
```python
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
plt.savefig("output.png")
|
||||
```
|
||||
|
||||
`Seaborn` is particularly well-suited for exploratory data analysis (EDA), making it easy to identify patterns and relationships in data with its diverse plotting functions and beautiful default styles. This guide introduces the basics of creating various types of plots with Seaborn, but the library's capabilities are much broader, offering sophisticated options for customizing plots and analyzing complex datasets.
|
||||
|
||||
|
||||
Seaborn’s integration with pandas DataFrames enhances its usability in the data science workflow, making it an indispensable tool for data visualization and exploratory data analysis.
|
||||
Reference in New Issue
Block a user