Matplotlib works hand in glove with pandas and NumPy. You can plot arrays directly, or let pandas draw onto a Matplotlib Axes you control — getting convenience and customisation.
pandas .plot onto your Axes
Pass ax= to a pandas plot so it draws on your Axes — then customise with Matplotlib:
fig, ax = plt.subplots(figsize=(10, 5))df.groupby("month")["sales"].sum().plot(kind="bar", ax=ax)ax.set_title("Monthly sales") # Matplotlib customisationax.set_ylabel("₹ lakh")ax.spines[["top", "right"]].set_visible(False)
Plotting NumPy arrays
import numpy as npx = np.linspace(0, 10, 200)ax.plot(x, np.sin(x))ax.imshow(np.random.rand(20, 20), cmap="viridis") # a matrix as an image
Key takeaways
- 01Matplotlib plots NumPy arrays and pandas objects directly.
- 02Pass ax= to df.plot / Series.plot so pandas draws on your Axes.
- 03Then apply any Matplotlib customisation to that Axes.
- 04imshow renders a 2-D array as an image/heatmap.
