matplotlib

Introduction Reading Time: 12 min

Table of Contents

Description

Matplotlib is a popular Python library used for data visualization. It provides an object-oriented API to embed plots into applications. The most commonly used module in Matplotlib is pyplot, which simplifies creating plots such as line charts, bar charts, histograms, scatter plots, and more.

Prerequisites

  • Basic knowledge of Python functions and lists
  • Familiarity with NumPy or Pandas (for plotting data)
  • Understanding of chart types like line and bar charts

Examples

Here's a simple example of a data science task using Python:


import matplotlib.pyplot as plt

# Line Chart Example
x = [1, 2, 3, 4, 5]
y = [10, 20, 25, 30, 40]

# Basic line plot
plt.plot(x, y, label="Growth", color="blue", marker='o')
plt.title("Line Chart Example")         # Add title
plt.xlabel("Time")                      # X-axis label
plt.ylabel("Value")                     # Y-axis label
plt.legend()                            # Show legend
plt.grid(True)                          # Add grid
plt.show()

# Bar Chart Example
categories = ['A', 'B', 'C', 'D']
values = [20, 35, 30, 10]

plt.bar(categories, values, color='green')
plt.title("Bar Chart Example")
plt.xlabel("Categories")
plt.ylabel("Values")
plt.show()
          

Real-World Applications

Data Reporting

Visualizing trends over time (line charts)
Category-based summaries (bar charts)

Business Intelligence

Monthly sales tracking
Product-wise revenue comparison

Where topic Is Applied

Healthcare

  • Number of patients over time
  • Comparison of test types or diseases per region

Finance

  • Stock price trends (line plots)
  • Revenue comparison by department (bar charts)

Resources

Data Science topic PDF

Download

Harvard Data Science Course

Free online course from Harvard covering data science foundations

Visit

Interview Questions

➤ Matplotlib is a data visualization library in Python used to create static, animated, and interactive plots.

➤ Line plots, bar charts, scatter plots, histograms, pie charts.

➤ It displays the plot window. Without it, the plot may not appear in some environments.

➤ Use plt.xlabel("X Label") and plt.ylabel("Y Label").

➤ Yes, using parameters like color, linestyle, and marker.