Data Visualization with Matplotlib and Seaborn

Part 4 of the Python for Data Science track. Last updated: September 2026.

A table of numbers convinces nobody; a chart does. Matplotlib is the foundation — full control, verbose. Seaborn sits on top — prettier defaults, statistical plots in one line. Learn both.

Install them in your virtual environment with:

pip install matplotlib seaborn

Your first plot: line chart

import matplotlib.pyplot as plt

months = ["Jan", "Feb", "Mar", "Apr"]
revenue = [120, 150, 130, 180]

plt.plot(months, revenue, marker="o", label="2026")
plt.title("Monthly Revenue")
plt.xlabel("Month")
plt.ylabel("Revenue ($k)")
plt.legend()
plt.show()

Every chart needs a title, axis labels, and a legend — unlabeled charts are a rookie tell.

Bar charts

products = ["A", "B", "C"]
units = [45, 78, 62]

plt.bar(products, units, color="green")
plt.title("Units Sold by Product")
plt.ylabel("Units")
plt.show()

Scatter plots and histograms

import numpy as np

x = np.random.rand(200)
y = 2 * x + np.random.randn(200) * 0.2   # y roughly follows x, with noise

plt.scatter(x, y, alpha=0.5)             # alpha: see through overlapping dots
plt.title("Scatter: does y follow x?")
plt.show()

plt.hist(y, bins=20)                     # distribution shape at a glance
plt.title("Distribution of y")
plt.show()

Seaborn: better defaults, statistical plots

import seaborn as sns
import pandas as pd

sns.set_theme(style="whitegrid")   # one line upgrades every plot's look

df = pd.DataFrame({"a": [1, 2, 3, 4],
                   "b": [2, 4, 5, 4],
                   "c": [5, 3, 4, 2]})
sns.heatmap(df.corr(), annot=True, cmap="Greens")  # correlation matrix as colors
plt.title("Correlation Heatmap")
plt.show()

Pairplots and saving figures

tips = sns.load_dataset("tips")   # built-in sample dataset (downloads once)
sns.pairplot(tips, hue="sex")      # every numeric column vs every other, colored by sex
plt.savefig("pairplot.png", dpi=150)   # save to file — use in reports, not just show()
plt.show()

Key takeaways

  • Matplotlib = control; Seaborn = speed and beauty. Use both.
  • Line for trends, bar for categories, scatter for relationships, hist for distributions.
  • Always add title, labels, and legend.
  • savefig() turns an exploratory plot into a report asset.

Next in this series: SQL with Python: sqlite3, pandas and SQLAlchemy Basics.

Comments

Popular posts from this blog

Java Banking Finance Services and Insurance (BFSI) domain interview questions

JSP Servlet Interview Questions For Freshers Series 1

Java program to check even or odd number