Skip to main content
Albero Academy

Pandas & NumPy Integration

Plot straight from DataFrames and arrays with the Axes you control.

Applied Matplotlib8 min

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:

best of both
fig, ax = plt.subplots(figsize=(10, 5))
df.groupby("month")["sales"].sum().plot(kind="bar", ax=ax)
ax.set_title("Monthly sales") # Matplotlib customisation
ax.set_ylabel("₹ lakh")
ax.spines[["top", "right"]].set_visible(False)

Plotting NumPy arrays

arrays are first-class
import numpy as np
x = 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

  1. 01Matplotlib plots NumPy arrays and pandas objects directly.
  2. 02Pass ax= to df.plot / Series.plot so pandas draws on your Axes.
  3. 03Then apply any Matplotlib customisation to that Axes.
  4. 04imshow renders a 2-D array as an image/heatmap.

Finished this chapter?

Mark it complete to track your progress through Matplotlib.