Seaborn Module

Seaborn is a Python library built on Matplotlib, designed for statistical data visualization. It provides an intuitive interface for creating attractive and informative graphics, making it popular in data analysis and machine learning projects.

Key Topics

Installing Seaborn

To use Seaborn, install it using pip. Ensure that dependencies like Matplotlib and NumPy are installed as well.

Example

# Install Seaborn
!pip install seaborn

Output

Successfully installed seaborn-0.X.X

Explanation: The !pip install seaborn command installs Seaborn along with its dependencies.

Creating a Basic Plot

Seaborn simplifies the process of creating plots. For example, you can use the sns.scatterplot() function to create a scatter plot of rainfall data in Chennai and Coimbatore.

Example

# Creating a scatter plot
import seaborn as sns
import matplotlib.pyplot as plt

# Data
city = ["Chennai", "Coimbatore", "Madurai", "Salem", "Trichy"]
rainfall = [200, 150, 100, 180, 130]

# Plot
sns.scatterplot(x=city, y=rainfall)
plt.title("Rainfall in Tamil Nadu Cities")
plt.xlabel("City")
plt.ylabel("Rainfall (mm)")
plt.show()

Output

A scatter plot showing rainfall in Tamil Nadu cities.

Explanation: The sns.scatterplot() function creates a scatter plot with city names on the x-axis and rainfall on the y-axis. Titles and labels are added using Matplotlib functions.

Customizing Seaborn Plots

Seaborn allows you to customize plots with themes, palettes, and additional options. For example, you can use set_theme() to change the appearance of all plots.

Example

# Customizing Seaborn plots
sns.set_theme(style="darkgrid")

# Create a line plot
temp = [30, 32, 33, 31, 29]
days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]

sns.lineplot(x=days, y=temp)
plt.title("Weekly Temperature in Chennai")
plt.xlabel("Day")
plt.ylabel("Temperature (°C)")
plt.show()

Output

A line plot with a dark grid theme showing weekly temperatures.

Explanation: The set_theme() function sets the plot style. Here, a dark grid is used to improve readability for the line plot of weekly temperatures.

Key Takeaways

  • Easy Installation: Install Seaborn using pip and start creating advanced visualizations.
  • Intuitive Interface: Functions like scatterplot() and lineplot() make it easy to visualize data.
  • Customization: Use set_theme() and other options for aesthetic enhancements.
  • Applications: Create insightful plots for datasets, such as rainfall and temperature trends.