Skip to main content
Albero Academy

Plotting with pandas

Turn DataFrames into charts directly with .plot — line, bar, histogram and scatter — for fast exploratory visuals.

Chapter 14Pandas14 min readBeginner friendly
  • #Pandas
  • #Plotting
  • #Visualization

For exploration you don't need a separate charting script — pandas plots straight from a DataFrame via .plot (backed by matplotlib). It's built for the quick "let me see it" visuals that guide analysis, not final presentation graphics.

The .plot method

one line to a chart
import matplotlib.pyplot as plt
ts["sales"].plot() # line chart (index on x)
df["package"].plot(kind="hist", bins=20) # distribution
plt.show()

Chart kinds

kindShows
lineTrend over an ordered axis (time)
bar / barhCompare categories
histDistribution of one numeric column
boxSpread & outliers across groups
scatterRelationship between two numerics
area / pieComposition
picking the right chart
df.groupby("programme")["package"].mean().plot(kind="bar")
df.plot(kind="scatter", x="experience", y="package")
df.boxplot(column="package", by="programme")

Plotting grouped data

Aggregate first, then plot — the aggregated table is the chart data:

aggregate → plot
(df.groupby("year")["sales"].sum()
.plot(kind="line", marker="o", title="Sales by year"))
# unstack a two-key group into a grouped/stacked bar
(df.groupby(["year", "programme"])["package"].mean()
.unstack("programme")
.plot(kind="bar"))

Quick polish

labels & size
ax = df["package"].plot(kind="hist", bins=15, figsize=(8, 4),
title="Package distribution")
ax.set_xlabel("Package (LPA)")
ax.set_ylabel("Students")
plt.tight_layout()

Practice

Plot a time series as a line chart, a numeric column as a histogram, and a group average as a bar chart. Make a scatter of two numeric columns and eyeball the relationship. Add a title and axis labels to one of them.

Key takeaways

  1. 01df.plot() / series.plot() chart directly from pandas, backed by matplotlib.
  2. 02Pick kind by intent: line (trend), bar (compare), hist/box (distribution), scatter (relationship).
  3. 03Aggregate with groupby first; the resulting table is the chart data (unstack for grouped bars).
  4. 04Set title, figsize and axis labels via the returned Axes for a quick polish.
  5. 05Use pandas plotting to explore; switch to seaborn/matplotlib/Plotly for presentation.