Charts Anyone Can Read
Your charts work, but would a stranger understand them? A bare chart is a puzzle: bars of what, measured in what? Today you make charts anyone can read โ with a title, labeled axes, and a splash of color โ and save one to share.
Open this lesson in Colab๐ก Load the data and chart tool first:
import pandas as pd import seaborn as sns import matplotlib.pyplot as plt penguins = sns.load_dataset("penguins").dropna()
A title and labels
After drawing a chart, you add words with plt.title, plt.xlabel, and plt.ylabel. The chart isnโt shown until plt.show(), so put these before it:
penguins.groupby("species")["body_mass_g"].mean().plot(kind="bar")
plt.title("Average penguin weight by species")
plt.xlabel("Species")
plt.ylabel("Body mass (grams)")
plt.show()
Now the chart explains itself: anyone can see itโs about penguin weight, by species, in grams. Labels turn a picture into a message.
Try it ๐ฏ
Take your flipper-length-per-species chart and give it a title and both axis labels.
Add some color
.plot(...) accepts a color:
penguins["species"].value_counts().plot(kind="bar", color="teal")
plt.title("How many penguins of each species")
plt.xlabel("Species")
plt.ylabel("Count")
plt.show()
Try "coral", "slateblue", "gold" โ pick something that fits your story.
Make it bigger
A roomier chart is easier to read. Set the size with figsize=(width, height) (in inches):
penguins.plot(kind="scatter", x="flipper_length_mm", y="body_mass_g", figsize=(8, 5))
plt.title("Heavier penguins have longer flippers")
plt.xlabel("Flipper length (mm)")
plt.ylabel("Body mass (grams)")
plt.show()
Notice the title itself states the finding โ a great habit for a data report.
Save your chart
Save a chart to an image file with plt.savefig(...) โ put it just before plt.show():
penguins["species"].value_counts().plot(kind="bar", color="teal")
plt.title("Penguins by species")
plt.savefig("penguins_by_species.png")
plt.show()
In Colab, the file appears in the Files panel on the left (the little folder icon) โ right-click it to download, and youโve got an image to put in a report or show a friend.
Predict it ๐ฎ
If you run plt.title(...) after plt.show(), will the title appear on the chart? (No โ plt.show() finishes and displays the chart, so anything after it is too late. Always label before show.)
Fix the bug ๐
This chart is a mystery โ bars with no explanation. Add a title and axis labels so a stranger could understand it:
penguins.groupby("island")["body_mass_g"].mean().plot(kind="bar")
plt.show()
(Before plt.show(), add plt.title("Average weight by island"), plt.xlabel("Island"), and plt.ylabel("Body mass (grams)").)
Your mission ๐
Make one polished chart: pick a question, draw the chart, give it a clear title (state the finding!), label both axes, choose a color, and savefig it. Then download the image from the Files panel.
What you learned today
plt.title,plt.xlabel,plt.ylabelexplain a chart โ add them beforeplt.show().color=andfigsize=make charts prettier and clearer.plt.savefig("name.png")saves a chart to share.- A good title states the finding, not just the topic.
Next time, you break free of penguins and load a dataset about something you care about. ๐
Comments