Stop 03 · Python for Data ScienceTopics · 5/9

Matplotlib & Seaborn

Part of the Data Science Roadmap

In short

A table of 50 numbers hides the story. One plot shows the trend, the outlier and the gap in seconds, for you first and for your audience later.

Python's core plotting libraries — Matplotlib gives full control over every chart element, Seaborn builds on top of it with better statistical chart defaults out of the box.

The jaadu makes a colourful rangoli using a stencil and coloured powder.

Think of it like this

Rangoli by hand or with a stencil

You can draw a rangoli dot by dot, controlling every line. Or you lay a stencil and get a beautiful pattern quickly, then touch it up by hand. Matplotlib is the by-hand control. Seaborn is the stencil built on top of it.

Watch it happen

Which one do I reach for?

Matplotlib

  • Full control over every element
  • More code for nice looks
  • Base layer for most Python plots

Seaborn

  • Good-looking statistical charts fast
  • Works directly with DataFrame columns
  • Built on Matplotlib, so tweakable

Start with Seaborn for exploring, then use Matplotlib calls to fix titles, labels and limits before sharing.

The big ideas

What you need to know

Figure and axes

A figure is the canvas; an axes is one chart on it. fig, ax = plt.subplots() gives you both.

Match chart to question

Trend over time: line. Compare groups: bar. Distribution: histogram or box. Relationship: scatter.

hue= adds a category

In Seaborn, hue="city" colours points or bars by city without any loops.

Label everything

Title, axis labels with units, readable ticks. An unlabeled chart forces people to guess.

See it in code

Walk through the code

python
1import matplotlib.pyplot as plt2import seaborn as sns3 4fig, ax = plt.subplots(figsize=(8, 4))5sns.barplot(data=summary, x="month", y="revenue", hue="city", ax=ax)6 7ax.set_title("Monthly revenue by city")8ax.set_xlabel("Month")9ax.set_ylabel("Revenue (₹)")10plt.tight_layout()11fig.savefig("revenue.png", dpi=150)

1/4Create the canvas and one chart area, 8 by 4 inches.

Seaborn draws the chart in one line, Matplotlib polishes it, and savefig exports it for slides.

Save yourself the pain

Common mistakes

Oops

A pie chart with twelve thin slices.

Do this instead

Use a sorted bar chart. People compare bar lengths far better than slice angles.

Oops

Default charts pasted into a report with no title or units.

Do this instead

Add a title that states the finding and axis labels with units like ₹ or %.

Your mission

Four charts, four questions

~30 min

Quick check

You want to see how delivery times are spread out, including any long tail. Best first chart?

Go deeper

  1. 1Build the same chart in both libraries and compare code and output.
  2. 2Practice a Seaborn pairplot and correlation heatmap for quick EDA.
  3. 3Learn to customize titles, labels and figure size for presentation-ready charts.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge