Matplotlib#

Matplotlib is a comprehensive library in Python for creating static, animated, and interactive visualizations. It’s widely used for data visualization in the scientific and engineering communities. Below is an introduction to Matplotlib, covering installation, basic usage, and some example plots.

import matplotlib.pyplot as plt

Line plot#

# Data for plotting
x = [1, 2, 3, 4, 5]
y = [2, 3, 5, 7, 11]

# Create a plot
plt.plot(x, y)

# Add a title and labels
plt.title('Simple Line Plot')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')

# Show the plot
plt.show()
../_images/7cf7217aab7158e27146f3c8ccd83d042704428515f5cc572cb56dcee4f2a6d4.png

Bar plot#

# Data for bar plot
categories = ['A', 'B', 'C']
values = [10, 20, 15]

plt.bar(categories, values)
plt.title('Bar Plot')
plt.show()
../_images/f5994b49eb7c79601d93fbd0d10bd0a29f142aefc9ed7fa40ffa28ee9ead3308.png

Scatter plot#

# Data for scatter plot
x = [1, 2, 3, 4, 5]
y = [2, 3, 5, 7, 11]

plt.scatter(x, y)
plt.title('Scatter Plot')
plt.show()
../_images/b31bce7a0224f734481fecbd35073aaf37ae521bf4a7b374afc47c07a350153f.png

Histogram#

# Data for histogram
data = [1, 1, 2, 3, 3, 3, 4, 4, 5, 5, 5, 5]

plt.hist(data, bins=5)
plt.title('Histogram')
plt.show()
../_images/82397d8c1a8539d8fc2072ef8e2e0c21a1482f12fb3e20d619b1da682399cc01.png