Lesson 31 of 60 · python
Matplotlib Basics: Plotting with Python
Duration: 15 minutes
Matplotlib Basics
Matplotlib is the foundational plotting library in Python. It works well for static images and integrates tightly with Pandas.
Importing the library
import matplotlib.pyplot as plt
Simple line plot
x = [1, 2, 3, 4, 5]
y = [2, 3, 5, 7, 11]
plt.plot(x, y, marker='o', linestyle='-')
plt.title('Simple Line Plot')
plt.xlabel('X axis')
plt.ylabel('Y axis')
plt.show()
Saving a figure
plt.savefig('images/line_plot.png', dpi=300, bbox_inches='tight')
The Figure and Axes objects
fig, ax = plt.subplots(figsize=(6,4))
ax.plot(x, y, color='purple')
ax.set_title('Figure & Axes Example')
plt.show()