Your First Chart


Numbers are great, but a picture makes patterns jump out. Today you make your first chart. The tool is matplotlib, and pandas makes it so easy you can draw a chart in a single line.

Open this lesson in Colab

💡 Load the data and the chart tool first:

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
penguins = sns.load_dataset("penguins").dropna()

In Colab, charts appear right under the cell.

A bar chart of counts

Remember value_counts()? Add .plot(kind="bar") and it becomes a chart:

penguins["species"].value_counts().plot(kind="bar")
plt.show()

Run it, and a bar chart pops up — one bar per species, taller bars for more penguins. The same numbers from before, now a picture you can read at a glance.

Try it 🎯

Make a bar chart of how many penguins live on each island.

A bar chart of averages

Pair groupby with .plot(kind="bar") to chart a value per group — like the average weight of each species:

penguins.groupby("species")["body_mass_g"].mean().plot(kind="bar")
plt.show()

Now you can see that Gentoos are heavier: the bar is clearly tallest. A chart turns a table of numbers into an instant comparison.

Try it 🎯

Chart the average flipper_length_mm per species.

Reading a chart

A bar chart is for comparing categories. Taller bar = bigger number. Your eye does the comparison instantly; that’s the whole point of a chart. (Soon we’ll add titles and labels so anyone can understand it.)

Predict it 🔮

Before running the average-mass chart: which species’ bar will be tallest? (Gentoo: they’re the heaviest species, so their average-mass bar towers over the others.)

Fix the bug 🐞

This is supposed to draw a chart, but nothing appears (or it shows raw numbers). It’s missing the .plot(...) part:

penguins["species"].value_counts()

(That just prints the counts. To draw them, add .plot(kind="bar") and plt.show().)

Your mission 🚀

Make two charts in your notebook: (1) a bar chart of the count of each sex, and (2) a bar chart of the average body_mass_g per island. Look at each and say (as a print) one thing it tells you.

What you learned today

  • matplotlib (import matplotlib.pyplot as plt) draws charts; pandas makes it one line.
  • something.plot(kind="bar") turns counts or grouped averages into a bar chart.
  • A bar chart compares categories — taller bar, bigger number.
  • A picture reveals patterns a table hides.

Next time, more kinds of charts — lines, dots, and histograms — and how to pick the right one for your question. 📉

Comments