Matplotlib Guide With Code
Matplotlib Guide With Code
x = [1, 2, 3, 4, 5]
y = [10, 12, 5, 8, 7]
plt.plot(x, y)
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.grid(True)
plt.show()
Bar Plot
Bar plots are used to represent categorical data with rectangular bars.
# Bar Plot
plt.bar(x, y, color='skyblue')
plt.title('Bar Chart')
plt.show()
Scatter Plot
# Scatter Plot
x = [1, 2, 3, 4, 5]
y = [2, 3, 5, 7, 11]
plt.scatter(x, y, color='red')
plt.title('Scatter Plot')
Matplotlib Guide with Code and Explanations
plt.show()
Histogram
# Histogram
import numpy as np
data = np.random.randn(1000)
plt.title('Histogram')
plt.show()
Pie Chart
# Pie Chart
plt.axis('equal')
plt.title('Pie Chart')
plt.show()
Subplots
# Subplots
x = [1, 2, 3, 4, 5]
y = [10, 12, 5, 8, 7]
axs[0, 0].plot(x, y)
axs[0, 1].bar(x, y)
axs[1, 0].scatter(x, y)
Matplotlib Guide with Code and Explanations
plt.tight_layout()
plt.show()
Object-Oriented Interface
This is a more flexible way to build plots using Axes and Figure objects.
# OO Interface
fig, ax = plt.subplots()
ax.plot(x, y)
ax.set_title('Using OO Interface')
ax.set_xlabel('X')
ax.set_ylabel('Y')
plt.show()