SwadhyAI Notes

All Python notes

Matplotlib Getting Started 🚀

Your First Plot

Most Matplotlib utilities lie under the pyplot submodule, usually imported as plt.

✨ Basic Line Plot

Let's draw a simple line from position (0,0) to (6,250).

python
import matplotlib.pyplot as plt
import numpy as np

xpoints = np.array([0, 6])
ypoints = np.array([0, 250])

plt.plot(xpoints, ypoints)
plt.show()

🖼️ Anatomy of a Figure

Understanding the terminology is key:

  • Figure: The whole window or image.
  • Axes: The area where the plot is drawn (contains x-axis, y-axis, title). A Figure can have multiple Axes (subplots).
  • Axis: The number-line-like objects (x-axis, y-axis).
  • Label: The names of the axes (x-label, y-label).

✨ Pro Tip: plt.show()

In Jupyter Notebooks, plots are often displayed automatically. However, in standard Python scripts, you must call plt.show() to open the window and display the plot.