0% found this document useful (0 votes)
23 views5 pages

SEABORN Visualizations

The document provides a comprehensive guide on creating visualizations using Seaborn, including simple, intermediate, and advanced plots such as histograms, scatter plots, and violin plots. It emphasizes the importance of customizing themes, color palettes, and adding titles and labels for clarity. Additionally, it includes templates for popular charts and instructions for exporting visualizations.

Uploaded by

toptech324
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views5 pages

SEABORN Visualizations

The document provides a comprehensive guide on creating visualizations using Seaborn, including simple, intermediate, and advanced plots such as histograms, scatter plots, and violin plots. It emphasizes the importance of customizing themes, color palettes, and adding titles and labels for clarity. Additionally, it includes templates for popular charts and instructions for exporting visualizations.

Uploaded by

toptech324
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

SEABORN visualizations

Import Required Libraries


import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

# Load your CSV data


file_path = "your_dataset.csv" # Replace with your file path
data = pd.read_csv(file_path)

# Quick preview
print(data.head())

1. Simple Plots

a. Histogram (Univariate Analysis)


# Plot a histogram for numerical columns
sns.histplot(data['column_name'], kde=True) # Replace 'column_name' with a
numeric column
plt.title("Histogram")
plt.show()
b. Bar Plot (Categorical Data)
sns.countplot(data=data, x='category_column') # Replace 'category_column'
with a categorical column
plt.title("Count Plot")
plt.show()
c. Box Plot
sns.boxplot(data=data, y='column_name') # Replace 'column_name' with a
numeric column
plt.title("Box Plot")
plt.show()

2. Intermediate Plots

a. Scatter Plot (Bivariate Analysis)


sns.scatterplot(data=data, x='x_column', y='y_column') # Replace with column
names
plt.title("Scatter Plot")
plt.show()
b. Correlation Heatmap
plt.figure(figsize=(8, 6))
sns.heatmap(data.corr(), annot=True, cmap="coolwarm") # Works for numerical
columns
plt.title("Correlation Heatmap")
plt.show()
c. Pairplot (Multivariate Analysis)
sns.pairplot(data) # Automatically plots relationships between all numerical
columns
plt.show()

3. Advanced Plots

a. Violin Plot
sns.violinplot(data=data, x='category_column', y='numeric_column') # Replace
with column names
plt.title("Violin Plot")
plt.show()

b. Boxen Plot (For Large Datasets)


sns.boxenplot(data=data, x='category_column', y='numeric_column') # Replace
with column names
plt.title("Boxen Plot")
plt.show()

c. FacetGrid (Multiple Plots)


g = sns.FacetGrid(data, col='category_column') # Replace 'category_column'
with a categorical column
g.map(sns.histplot, 'numeric_column') # Replace 'numeric_column' with a
numeric column
plt.show()

d. Line Plot (Time Series or Trends)


sns.lineplot(data=data, x='x_column', y='y_column') # Replace with column
names
plt.title("Line Plot")
plt.show()

Key Points:

 Replace placeholder column names (column_name, x_column, y_column, etc.) with actual
column names from your dataset.
 Use data.info() or data.head() to understand your data structure and identify column
types.
 Gradually explore these plots depending on the complexity and insights needed.
1. Set Themes

Seaborn offers five built-in themes:

 darkgrid: Default with gridlines.


 whitegrid: White background with gridlines.
 dark: Dark background.
 white: White background without gridlines.
 ticks: Adds ticks to the axes.

# Example: Applying different themes


import seaborn as sns

sns.set_theme(style="whitegrid") # Choose: darkgrid, whitegrid, dark, white,


ticks

2. Use Color Palettes

Seaborn has built-in color palettes that you can use for a variety of effects:

 Default: deep
 Sequential: Blues, Reds, etc.
 Diverging: coolwarm, seismic, etc.
 Qualitative: Set1, Set2, Pastel1, etc.

# Example: Using a color palette


sns.set_palette("coolwarm") # Replace with your desired palette

# Example: Custom color palette


custom_palette = sns.color_palette("viridis", as_cmap=True)
sns.set_palette(custom_palette)

3. Add Titles, Labels, and Legends

Always include descriptive titles, axis labels, and legends for clarity.

import matplotlib.pyplot as plt

# Example: Adding title, labels, and legend


sns.scatterplot(data=data, x='x_column', y='y_column', hue='category_column')
# Replace with actual column names
plt.title("Scatter Plot of X vs Y", fontsize=14)
plt.xlabel("X Axis Label", fontsize=12)
plt.ylabel("Y Axis Label", fontsize=12)
plt.legend(title="Category")
plt.show()
4. Change Figure Size

Adjust the figure size for better visualization.

# Example: Changing figure size


plt.figure(figsize=(10, 6)) # Width x Height
sns.boxplot(data=data, x='category_column', y='numeric_column') # Replace
with column names
plt.title("Box Plot", fontsize=14)
plt.show()

5. Add Style Elements

Seaborn allows you to control specific elements for finer styling.

a. Set Axis Spines


sns.despine() # Removes top and right spines for cleaner look
b. Change Font Size and Style
sns.set_context("notebook", font_scale=1.2) # Context: paper, notebook, talk,
poster
c. Grid Customization
sns.set_style("whitegrid", {'grid.color': '.8', 'grid.linestyle': '--'}) #
Custom grid style

Templates for Popular Charts


a. Pairplot with Custom Styling
sns.set_theme(style="ticks")
sns.pairplot(data, hue="category_column", palette="husl", diag_kind="kde") #
Replace with column names
plt.show()
b. Heatmap with Annotations
plt.figure(figsize=(10, 8))
sns.heatmap(data.corr(), annot=True, fmt=".2f", cmap="coolwarm",
linewidths=0.5)
plt.title("Correlation Heatmap", fontsize=14)
plt.show()
c. Advanced Violin Plot
plt.figure(figsize=(10, 6))
sns.violinplot(data=data, x='category_column', y='numeric_column',
hue='group_column', split=True, palette="muted") # Replace with column names
plt.title("Violin Plot", fontsize=14)
plt.show()
d. Line Plot for Trends
plt.figure(figsize=(12, 6))
sns.lineplot(data=data, x='x_column', y='y_column', hue='category_column',
palette="tab10") # Replace with column names
plt.title("Trend Line Plot", fontsize=14)
plt.show()

7. Export Your Visualizations

Save the generated visualizations for use in presentations or reports.

# Save the plot as a file


plt.savefig("plot_name.png", dpi=300, bbox_inches='tight') # Save as PNG

Example Combining Styles and Templates


import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

# Load dataset
data = pd.read_csv("your_dataset.csv") # Replace with your file

# Set Theme and Palette


sns.set_theme(style="whitegrid", palette="viridis")

# Pairplot with Style


sns.pairplot(data, hue="category_column", diag_kind="kde", corner=True) #
Replace 'category_column'
plt.suptitle("Pairplot of Features", y=1.02, fontsize=16)
plt.show()

# Heatmap
plt.figure(figsize=(12, 8))
sns.heatmap(data.corr(), annot=True, fmt=".2f", cmap="coolwarm",
linewidths=0.5)
plt.title("Correlation Heatmap", fontsize=18)
plt.show()

You might also like