Seaborn Tutorial: Plotting in Python

Introduction

Seaborn is a Python visualization library based on Matplotlib that provides a high-level interface for drawing attractive and informative statistical graphics. It is particularly useful for exploring and understanding data.

Here are brief descriptions of each dataset used in the tutorial:

fMRI: Functional Magnetic Resonance Imaging (fMRI) datasets capture brain activity by detecting changes in blood flow, often used in neuroscience research to study brain functions.

Iris: The Iris dataset contains measurements of sepal length, sepal width, petal length, and petal width for three species of iris flowers, commonly used for classification and clustering tasks in machine learning.

Tips: The Tips dataset records information about tips given by customers in a restaurant, including total bill, tip amount, sex of the bill payer, day, time, and size of the dining party, often used for statistical analysis and regression.

Flights: The Flights dataset includes details about flights, such as departure and arrival times, delays, airline, and flight number, commonly used to analyze and predict flight performance and delays.

Table of Contents

Installation

pip install seaborn

Basic Plotting with Seaborn

Question: How do you create a simple line plot using Seaborn?
Answer: A line plot can be created using the lineplot function. Here's an example:
import seaborn as sns
import matplotlib.pyplot as plt

# Sample data
data = sns.load_dataset('fmri')

# Create line plot
sns.lineplot(x='timepoint', y='signal', data=data)

# Display the plot
plt.show()
        
Line Plot

Advanced Plotting

Question: How can you create a scatter plot with Seaborn?
Answer: A scatter plot can be created using the scatterplot function. Here's an example:
import seaborn as sns
import matplotlib.pyplot as plt

# Sample data
data = sns.load_dataset('iris')

# Create scatter plot
sns.scatterplot(x='sepal_length', y='sepal_width', data=data)

# Display the plot
plt.show()
        
Scatter Plot

Customizing Plots

Question: How can you customize the color palette of a Seaborn plot?
Answer: You can customize the color palette using the palette parameter. Here's an example:
import seaborn as sns
import matplotlib.pyplot as plt

# Sample data
data = sns.load_dataset('tips')

# Create bar plot with custom palette
sns.barplot(x='day', y='total_bill', data=data, palette='coolwarm')

# Display the plot
plt.show()
        
Bar Plot with Custom Palette

Violin Plot

Question: What is a violin plot and how do you create one using Seaborn?
Answer: A violin plot is used to visualize the distribution of the data across different categories. It shows the density of the data at different values. Here's an example:
import seaborn as sns
import matplotlib.pyplot as plt

# Sample data
data = sns.load_dataset('tips')

# Create violin plot
sns.violinplot(x='day', y='total_bill', data=data)

# Display the plot
plt.show()
        
Violin Plot

Heatmap

Question: How can you create a heatmap with Seaborn?
Answer: A heatmap is used to show the magnitude of a phenomenon as color in two dimensions. Here's an example:
import seaborn as sns
import matplotlib.pyplot as plt

# Sample data
data = sns.load_dataset('flights')

# Create heatmap
heatmap_data = data.pivot_table(index='month', columns='year', values='passengers')
sns.heatmap(heatmap_data, annot=True, fmt="d", cmap='viridis')

# Display the plot
plt.show()
        
Heatmap

Swarm Plot

Question: What is a swarm plot and how do you create one using Seaborn?
Answer: A swarm plot is used to display the distribution of data points. It arranges the points to avoid overlap. Here's an example:
import seaborn as sns
import matplotlib.pyplot as plt

# Sample data
data = sns.load_dataset('tips')

# Create swarm plot
sns.swarmplot(x='day', y='total_bill', data=data)

# Display the plot
plt.show()
        
Swarm Plot

Distribution Plot

Question: How do you create a distribution plot using Seaborn?
Answer: A distribution plot shows the distribution of a dataset. You can create one using the distplot function. Here's an example:
import seaborn as sns
import matplotlib.pyplot as plt

# Sample data
data = sns.load_dataset('tips')

# Create distribution plot
sns.histplot(data['total_bill'], kde=True)

# Display the plot
plt.show()
        
Distribution Plot

Regression Plot

Question: How can you create a regression plot with Seaborn?
Answer: A regression plot shows the relationship between two variables along with a regression line. You can create one using the regplot function. Here's an example:
import seaborn as sns
import matplotlib.pyplot as plt

# Sample data
data = sns.load_dataset('tips')

# Create regression plot
sns.regplot(x='total_bill', y='tip', data=data)

# Display the plot
plt.show()
        
Regression Plot