0% found this document useful (0 votes)
20 views

Day 5 Data Visualisation With Maplotlib and Seaborn

Uploaded by

Deep gaichor
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Day 5 Data Visualisation With Maplotlib and Seaborn

Uploaded by

Deep gaichor
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Day 5: Data Visualization with Matplotlib and Seaborn

Plotting with Matplotlib: Line Plots, Bar Plots, Histograms: Matplotlib is a widely used
Python library for creating static, interactive, and animated visualizations. It supports various
plot types, including line plots, bar plots, histograms, scatter plots, etc.

# Example of creating line plot, bar plot, and histogram using Matplotlib
import matplotlib.pyplot as plt

# Line plot
plt.plot([1, 2, 3, 4], [1, 4, 9, 16])
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Line Plot')
plt.show()

# Bar plot
plt.bar(['A', 'B', 'C', 'D'], [10, 20, 15, 25])
plt.xlabel('Categories')
plt.ylabel('Values')
plt.title('Bar Plot')
plt.show()

# Histogram
data = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5]
plt.hist(data, bins=5)
plt.xlabel('Bins')
plt.ylabel('Frequency')
plt.title('Histogram')
plt.show()

Introduction to Seaborn for Statistical Plots: Seaborn is built on top of Matplotlib and
provides a high-level interface for drawing attractive and informative statistical graphics. It
simplifies the process of creating complex visualizations.

# Example of using Seaborn for statistical plots


import seaborn as sns

# Load example dataset


tips = sns.load_dataset("tips")

# Scatter plot with linear regression line


sns.lmplot(x="total_bill", y="tip", data=tips)
plt.xlabel('Total Bill')
plt.ylabel('Tip')
plt.title('Scatter Plot with Linear Regression')
plt.show()

# Box plot
sns.boxplot(x="day", y="total_bill", data=tips)
plt.xlabel('Day')
plt.ylabel('Total Bill')
plt.title('Box Plot')
plt.show()

# Violin plot
sns.violinplot(x="day", y="total_bill", data=tips)
plt.xlabel('Day')
plt.ylabel('Total Bill')
plt.title('Violin Plot')
plt.show()

Combining Multiple Plots: Matplotlib and Seaborn allow combining multiple plots in a
single figure to create complex visualizations for better data exploration and analysis.

# Example of combining multiple plots using Matplotlib


plt.subplot(1, 2, 1)
plt.plot([1, 2, 3, 4], [1, 4, 9, 16], 'r--')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Subplot 1')

plt.subplot(1, 2, 2)
plt.bar(['A', 'B', 'C', 'D'], [10, 20, 15, 25])
plt.xlabel('Categories')
plt.ylabel('Values')
plt.title('Subplot 2')

plt.tight_layout() # Adjust layout to prevent overlapping


plt.show()

Matplotlib and Seaborn are powerful visualization libraries that play a crucial role in
exploratory data analysis and communicating insights from data.
Day 5: Data Visualization with Matplotlib
and Seaborn
Plotting with Matplotlib: Line Plots, Bar Plots,
Histograms
Matplotlib:

 Versatile plotting library for Python


 Install with pip install matplotlib

Importing Matplotlib:
python
Copy code
import matplotlib.pyplot as plt

Line Plots:

 Example:

python
Copy code
x = [1, 2, 3, 4]
y = [10, 20, 25, 30]
plt.plot(x, y)
plt.xlabel('x-axis')
plt.ylabel('y-axis')
plt.title('Line Plot')
plt.show()

Bar Plots:

 Example:

python
Copy code
categories = ['A', 'B', 'C']
values = [10, 20, 30]
plt.bar(categories, values)
plt.xlabel('Categories')
plt.ylabel('Values')
plt.title('Bar Plot')
plt.show()

Histograms:

 Example:

python
Copy code
data = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5]
plt.hist(data, bins=5)
plt.xlabel('Bins')
plt.ylabel('Frequency')
plt.title('Histogram')
plt.show()

Introduction to Seaborn for Statistical Plots


Seaborn:

 Statistical data visualization library based on Matplotlib


 Install with pip install seaborn
 Provides high-level interface for drawing attractive and informative statistical
graphics

Importing Seaborn:
python
Copy code
import seaborn as sns

Scatter Plot:

 Example:

python
Copy code
sns.scatterplot(x='total_bill', y='tip', data=tips)
plt.xlabel('Total Bill')
plt.ylabel('Tip')
plt.title('Scatter Plot')
plt.show()

Box Plot:

 Example:

python
Copy code
sns.boxplot(x='day', y='total_bill', data=tips)
plt.xlabel('Day')
plt.ylabel('Total Bill')
plt.title('Box Plot')
plt.show()

Pair Plot:

 Example:

python
Copy code
sns.pairplot(tips)
plt.show()

Combining Multiple Plots


Subplots:

 Using plt.subplot() to create multiple plots in one figure


 Example:

plt.figure(figsize=(10, 5))

plt.subplot(1, 2, 1)
plt.plot(x, y)
plt.title('Line Plot')

plt.subplot(1, 2, 2)
plt.bar(categories, values)
plt.title('Bar Plot')

plt.tight_layout()
plt.show()

Example with Matplotlib and Seaborn:


import matplotlib.pyplot as plt
import seaborn as sns

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

# Creating a figure with subplots


plt.figure(figsize=(12, 6))

# Line Plot
plt.subplot(1, 3, 1)
x = [1, 2, 3, 4]
y = [10, 20, 25, 30]
plt.plot(x, y)
plt.title('Line Plot')

# Bar Plot
plt.subplot(1, 3, 2)
categories = ['A', 'B', 'C']
values = [10, 20, 30]
plt.bar(categories, values)
plt.title('Bar Plot')

# Scatter Plot with Seaborn


plt.subplot(1, 3, 3)
sns.scatterplot(x='total_bill', y='tip', data=tips)
plt.title('Scatter Plot')

plt.tight_layout()
plt.show()

This concludes the note for Day 5: Data Visualization with Matplotlib and Seaborn.

You might also like