AI / ML

Matplotlib

Data visualization

Matplotlib

Matplotlib is a comprehensive library for creating static, animated, and interactive visualizations in Python. It's the foundation for many other plotting libraries.

Install Matplotlib

pip install matplotlib

Import Matplotlib

import matplotlib.pyplot as plt
import numpy as np

Line Plot

x = np.linspace(0, 10, 100)
y = np.sin(x)

plt.plot(x, y)
plt.xlabel('X axis')
plt.ylabel('Y axis')
plt.title('Sine Wave')
plt.show()

Scatter Plot

x = np.random.randn(100)
y = np.random.randn(100)

plt.scatter(x, y)
plt.xlabel('X values')
plt.ylabel('Y values')
plt.title('Scatter Plot')
plt.show()

Bar Chart

categories = ['A', 'B', 'C', 'D']
values = [23, 45, 56, 78]

plt.bar(categories, values)
plt.xlabel('Categories')
plt.ylabel('Values')
plt.title('Bar Chart')
plt.show()

Histogram

data = np.random.normal(100, 15, 1000)

plt.hist(data, bins=30)
plt.xlabel('Value')
plt.ylabel('Frequency')
plt.title('Histogram')
plt.show()

Multiple Plots

fig, axes = plt.subplots(2, 2, figsize=(10, 8))

axes[0, 0].plot(x, y)
axes[0, 1].scatter(x, y)
axes[1, 0].bar(categories, values)
axes[1, 1].hist(data, bins=30)

plt.tight_layout()
plt.show()