Plotting in Python with Matplotlib and Seaborn

Creating clear static charts in Python: Matplotlib's figure and axes model and Seaborn's statistical plotting.

Intermediate · about 6 hours · difficulty 3/5

Learn first: pandas for Data Analysis, Choosing the Right Chart

Python plotting lets you create charts reproducibly, as part of your analysis code, at any scale.

Matplotlib: the foundation

  • Use the object-oriented style: fig, ax = plt.subplots(figsize=(8, 4)) then ax.plot(...), ax.bar(...), ax.set_title(...).
  • plt.subplots(2, 3) gives a grid of axes for small multiples.
  • Control ticks, labels and number formats (matplotlib.ticker), remove spines, and annotate key points with ax.annotate.
  • Save with fig.savefig("chart.png", dpi=200, bbox_inches="tight").

Seaborn: statistical charts fast

  • Works directly with tidy DataFrames: sns.histplot(data=df, x="price", hue="room_type").
  • Distribution plots (histplot, kdeplot, boxplot, violinplot, ecdfplot), relationship plots (scatterplot, lineplot, regplot), categorical plots (barplot, pointplot), and heatmap.
  • Figure-level functions (relplot, displot, catplot) create faceted small multiples with col= and row=.
  • Seaborn returns Matplotlib axes, so you can fine-tune with Matplotlib.

Style

Set a theme once (sns.set_theme(style="whitegrid")), then apply the design principles from earlier topics: highlight one thing, direct labels, insight titles.

Pitfall: Seaborn's barplot shows the mean with an error bar by default, not the sum. Know what each function aggregates. Move on when you can produce a publication-ready faceted chart with custom labels in a few lines.

Key concepts

  • Figure and Axes model
  • Line, bar, scatter in Matplotlib
  • Subplots and small multiples
  • Annotations and formatting
  • Seaborn distribution plots
  • Seaborn faceting with relplot and catplot
  • Themes and styling
  • plotnine / ggplot-style in Python

Mini project

Using the Seaborn 'tips' or Inside Airbnb data, produce a four-chart figure (distribution, trend, category comparison, relationship) with consistent styling and insight titles, saved as a PNG.

Check yourself

What does sns.barplot show by default, and how would you plot totals instead?

Free resources