Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

Matplotlib: Data Visualization

Lesson 27 of 37 in the free Data Science notes on Siksha Sarovar, written by Rohit Jangra.

Matplotlib: Data Visualization

Definition: Matplotlib is the most widely used library for creating static, animated, and interactive visualizations in Python. It provides fine-grained control over every aspect of a plot — from colors and labels to axes and legends.

import matplotlib.pyplot as plt

---

Why Matplotlib?

FeatureBenefit
VersatilityCreate virtually any type of chart
CustomizationFull control over every plot element
IntegrationWorks with NumPy, Pandas, Seaborn
Publication QualityProduces plots suitable for research papers
FoundationSeaborn and Pandas plotting are built on top of Matplotlib

---

The Anatomy of a Matplotlib Plot

A Matplotlib plot consists of:

  • Figure — The overall window/page.
  • Axes — The actual plot area (a Figure can have multiple Axes).
  • Title — The heading of the plot.
  • Labels — X and Y axis labels.
  • Legend — Identifies different data series.
  • Ticks — The marks along the axes.

---

Types of Plots

Plot TypeFunctionBest For
Line Plotplt.plot(x, y)Trends over time (time series)
Bar Chartplt.bar(x, y)Comparing categories
Horizontal Barplt.barh(x, y)Long category names
Histogramplt.hist(data, bins=10)Distribution of continuous data
Scatter Plotplt.scatter(x, y)Relationship between two variables
Pie Chartplt.pie(sizes, labels=labels)Proportions of a whole
Box Plotplt.boxplot(data)Distribution summary (median, quartiles, outliers)
Heatmapplt.imshow(data)Matrix/correlation visualization

---

Creating Plots

Line Plot:

x = [1, 2, 3, 4, 5]
y = [10, 20, 25, 30, 40]
plt.plot(x, y, color='blue', linestyle='--', marker='o')
plt.title("Sales Over Time")
plt.xlabel("Month")
plt.ylabel("Sales (₹)")
plt.grid(True)
plt.show()

Bar Chart:

categories = ['A', 'B', 'C', 'D']
values = [25, 40, 30, 55]
plt.bar(categories, values, color=['red', 'green', 'blue', 'orange'])
plt.title("Category Comparison")
plt.show()

Histogram:

import numpy as np
data = np.random.randn(1000)
plt.hist(data, bins=30, color='skyblue', edgecolor='black')
plt.title("Distribution of Random Data")
plt.xlabel("Value")
plt.ylabel("Frequency")
plt.show()

---

Customization Options

ElementCodeDescription
Titleplt.title("Title")Plot title
X Labelplt.xlabel("X Axis")X-axis label
Y Labelplt.ylabel("Y Axis")Y-axis label
Legendplt.legend()Show legend
Gridplt.grid(True)Toggle grid
Line Colorcolor='red'Change line color
Line Stylelinestyle='--'Dashed, dotted, etc.
Markermarker='o'Data point markers
Figure Sizeplt.figure(figsize=(10, 6))Width × Height in inches
Saveplt.savefig("plot.png", dpi=300)Save to file

---

Subplots (Multiple Plots)

Create multiple plots in a single figure:

fig, axes = plt.subplots(1, 2, figsize=(12, 5))

axes[0].plot([1, 2, 3], [10, 20, 30])
axes[0].set_title("Line Plot")

axes[1].bar(['A', 'B', 'C'], [5, 8, 3])
axes[1].set_title("Bar Chart")

plt.tight_layout()
plt.show()

---

Matplotlib with Pandas

Pandas DataFrames have built-in plotting that uses Matplotlib:

df['Score'].plot(kind='hist', bins=10, title='Score Distribution')
df.plot(x='Name', y='Score', kind='bar')
df.plot.scatter(x='Age', y='Score')

---

When to Use Matplotlib

ScenarioUse Matplotlib?
Quick exploratory plots✅ Yes
Publication-quality static plots✅ Yes
Full customization needed✅ Yes
Beautiful statistical plotsUse Seaborn (built on Matplotlib)
Interactive dashboardsUse Plotly or Dash

Summary

  • Matplotlib is the foundational visualization library in Python.
  • plt.plot(), plt.bar(), plt.hist(), and plt.scatter() are the most common plot types.
  • Every element of a plot (title, labels, colors, markers, grid) can be customized.
  • Subplots allow multiple plots in one figure.
  • Pandas integrates with Matplotlib for quick DataFrame plotting.