Seaborn Package

Seaborn is a powerful and easy-to-use Python data visualization library built on top of Matplotlib. It provides a high-level interface for drawing attractive and informative statistical graphics.

Introduction to Seaborn

Why Seaborn?

  • Built on top of matplotlib → better style and simplicity.

  • Works well with pandas DataFrames.

  • Built-in themes, color palettes, and functions for visualizing statistical relationships.

  • Automatic handling of data aggregation, bootstrapping, and confidence intervals.

📦 Installing Seaborn


pip install seaborn

🐍 Importing Seaborn in Python


import seaborn as sns

While Seaborn is most powerful when used with pandas DataFrames, you can use it directly with Python lists, NumPy arrays, or simple Python dictionaries too.


1. Scatter Plot (Lists)


import seaborn as sns import matplotlib.pyplot as plt x = [10, 20, 30, 40, 50] y = [5, 15, 25, 35, 45] sns.scatterplot(x=x, y=y) plt.title("Simple Scatter Plot") plt.show()


2. Line Plot (Lists)

x = [1, 2, 3, 4, 5] y = [2, 4, 6, 8, 10] sns.lineplot(x=x, y=y) plt.title("Simple Line Plot") plt.show()



3. Bar Plot (Dictionary)

categories = ['A', 'B', 'C', 'D'] values = [10, 20, 15, 25] sns.barplot(x=categories, y=values) plt.title("Bar Plot seaborn") plt.show()




4. Count Plot

If you don’t have a dataset but want a count of categories, repeat the elements:


data = ['apple', 'banana', 'apple', 'orange', 'banana', 'apple'] sns.countplot(x=data) plt.title("Count Plot seaborn") plt.show()



5. Histogram

data = [1, 1, 2, 2, 2, 3, 4, 5, 5, 5, 5, 6, 7] sns.histplot(data, bins=5, kde=True) plt.title("Histogram seaborn") plt.show()



6. Box Plot

data = [7, 15, 36, 39, 40, 41, 42, 43, 49, 50, 52] sns.boxplot(data=data) plt.title("Box Plot seaborn") plt.show()

        

7. Heat Map

import numpy as np matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) sns.heatmap(matrix, annot=True, cmap="YlGnBu") plt.title("Heatmap seaborn") plt.show()



Seaborn Built-in Datasets


Seaborn comes with built-in datasets for learning and testing. Use sns.get_dataset_names() to list them.


# Load example dataset tips = sns.load_dataset("tips") print(tips.head())

total_bill   tip     sex smoker  day    time  size
0       16.99  1.01  Female     No  Sun  Dinner     2
1       10.34  1.66    Male     No  Sun  Dinner     3
2       21.01  3.50    Male     No  Sun  Dinner     3
3       23.68  3.31    Male     No  Sun  Dinner     2
4       24.59  3.61  Female     No  Sun  Dinner     4


Basic Plots with Seaborn
1. Scatter Plot (sns.scatterplot)

Used to show relationship between two numerical variables.
sns.scatterplot(x='total_bill', y='tip', data=tips) plt.title("Scatter Plot: Total Bill vs Tip") plt.show()


2.Line Plot (sns.lineplot)


Useful for time series or ordered data. sns.lineplot(x='size', y='tip', data=tips)

plt.title("Line Plot: Size vs Tip") plt.show()


3. Bar Plot (sns.barplot)

Shows the mean value with confidence intervals by default.

sns.barplot(x='day', y='total_bill', data=tips) plt.title("Average Total Bill by Day") plt.show()


4. Count Plot (sns.countplot)

Used for showing the count of observations.

sns.countplot(x='day', data=tips) plt.title("Count of Records for Each Day") plt.show()

5. Box Plot (sns.boxplot)

Shows distribution summary with outliers.

sns.boxplot(x='day', y='total_bill', data=tips) plt.title("Box Plot: Total Bill by Day") plt.show()



6. Violin Plot (sns.violinplot)

Combines boxplot and KDE plot.

sns.violinplot(x='day', y='total_bill', data=tips) plt.title("Violin Plot: Total Bill by Day") plt.show()



7. Histogram (sns.histplot)

Shows frequency distribution.

sns.histplot(data=tips, x='total_bill', bins=20, kde=True) plt.title("Histogram of Total Bills") plt.show()


8. Pair Plot (sns.pairplot)

Great for visualizing pairwise relationships between multiple variables.

sns.pairplot(tips) plt.suptitle("Pairplot of Tips Dataset", y=1.02) plt.show()


9. Heatmap (sns.heatmap)

Good for showing correlation matrices.

corr = tips.corr() sns.heatmap(corr, annot=True, cmap='coolwarm') plt.title("Correlation Heatmap") plt.show()


Advanced Example: Hue, Style, Size

sns.scatterplot(x='total_bill', y='tip', hue='sex', style='time', size='size', data=tips) plt.title("Scatter Plot with Hue, Style, and Size") plt.show()
    

Comments

Popular posts from this blog

Python for Artificial Intelligence MNCST319 KTU BTech CS Minor 2024

Python for Artificial Intelligence MNCST319 KTU Minor 2024 - course details and syllabus

Python for Artificial Intelligence MNCST 319 KTU 2024 Scheme Minor Model Question Paper