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

Week8 PBD

Matplotlib is a versatile Python library for creating various types of visualizations, including line plots, area plots, histograms, bar charts, pie charts, box plots, and scatter plots. It allows for extensive customization and supports multiple output formats. The document provides installation instructions, basic plotting examples, and key functions for each plot type.

Uploaded by

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

Week8 PBD

Matplotlib is a versatile Python library for creating various types of visualizations, including line plots, area plots, histograms, bar charts, pie charts, box plots, and scatter plots. It allows for extensive customization and supports multiple output formats. The document provides installation instructions, basic plotting examples, and key functions for each plot type.

Uploaded by

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

📊 Lecture Notes on Matplotlib

1. Introduction to Matplotlib

Matplotlib is a comprehensive library for creating static, animated, and interactive visualizations in
Python.

Features:

2D plotting library.

Integration with NumPy and Pandas.

Supports output in many formats (PNG, PDF, SVG).

Highly customizable plots.

Installation:

pip install matplotlib

Importing:

import matplotlib.pyplot as plt

2. Basic Plotting with Matplotlib

Creating a Simple Plot:

import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]

y = [10, 20, 25, 30, 40]

plt.plot(x, y)

plt.title("Basic Plot")

plt.xlabel("X-axis")

plt.ylabel("Y-axis")

plt.grid(True)

plt.show()

Components of a Plot:

plt.plot(): Creates the line plot.

plt.title(): Adds a title.

plt.xlabel() and plt.ylabel(): Label axes.

plt.grid(): Optional grid.


plt.show(): Displays the figure.

3. Line Plots

Used to represent trends over time or continuous data.

Example:

x = [1, 2, 3, 4, 5]

y = [2, 3, 5, 7, 11]

plt.plot(x, y, color='green', linestyle='--', marker='o')

plt.title("Line Plot Example")

plt.show()

Customizations:

color='blue': Line color.

linestyle='--': Dashed line.

marker='o': Circular data points.

4. Area Plots

Used to show cumulative totals over time.

Example:

days = [1, 2, 3, 4, 5]

values = [3, 8, 1, 10, 5]

plt.fill_between(days, values, color='skyblue', alpha=0.4)

plt.plot(days, values, color='Slateblue', alpha=0.6)

plt.title("Area Plot")

plt.show()

Tip:

Use fill_between() to create area plots.

alpha controls transparency.

5. Histograms

Used to display the distribution of a dataset.

Example:

import numpy as np
data = np.random.randn(1000)

plt.hist(data, bins=30, color='purple', edgecolor='black')

plt.title("Histogram")

plt.xlabel("Value")

plt.ylabel("Frequency")

plt.show()

Parameters:

bins: Number of intervals.

color: Bar color.

edgecolor: Border color of bars.

6. Bar Charts

Used for categorical data comparison.

Example:

categories = ['A', 'B', 'C', 'D']

values = [23, 45, 56, 78]

plt.bar(categories, values, color='coral')

plt.title("Bar Chart")

plt.xlabel("Categories")

plt.ylabel("Values")

plt.show()

Horizontal Bar Chart:

plt.barh(categories, values, color='teal')

plt.title("Horizontal Bar Chart")

plt.show()

7. Pie Charts

Used to show proportions of a whole.

Example:

labels = ['Apples', 'Bananas', 'Cherries', 'Dates']

sizes = [30, 25, 20, 25]


colors = ['gold', 'yellowgreen', 'lightcoral', 'lightskyblue']

plt.pie(sizes, labels=labels, colors=colors, autopct='%1.1f%%', startangle=140)

plt.title("Pie Chart")

plt.axis('equal') # Equal aspect ratio ensures the pie is circular.

plt.show()

Parameters:

autopct: Display percentage.

startangle: Rotates start of pie chart.

axis('equal'): Keeps aspect ratio equal.

8. Box Plots

Used to show data distribution through quartiles.

Example:

data = [7, 15, 13, 18, 21, 30, 45, 40, 50, 60]

plt.boxplot(data)

plt.title("Box Plot")

plt.show()

Interpretation:

Box: IQR (Q1 to Q3)

Line inside box: Median

Whiskers: Min/Max (excluding outliers)

Dots: Outliers

9. Scatter Plots

Used to show relationship between two variables.

Example:

x = [1, 2, 3, 4, 5]

y = [5, 4, 3, 2, 1]

plt.scatter(x, y, color='red')

plt.title("Scatter Plot")

plt.xlabel("X-axis")
plt.ylabel("Y-axis")

plt.show()

Useful For:

Correlation analysis.

Clustering and outlier detection.

Table:

Plot Type Use Case Key Function

Line Plot Time series, trends plot()

Area Plot Cumulative data fill_between()

Histogram Data distribution hist()

Bar Chart Categorical comparison bar() / barh()

Pie Chart Proportional data pie()

Box Plot Statistical distribution boxplot()

Scatter Plot Relationship between variables scatter()

You might also like