What Is Matplotlib?
Matplotlib is a popular Python library for data visualization. It allows developers and data analysts to create a wide range of graphs and charts such as line charts, bar charts, histograms, pie charts, and scatter plots.
It is widely used in data science, machine learning, research, and analytics to visually understand patterns and trends in data.
Why Use Matplotlib?
Matplotlib is preferred because it:
-
Is open-source and free
-
Works seamlessly with NumPy and Pandas
-
Supports many chart types
-
Provides high customization
-
Produces publication-quality graphs
-
Is beginner-friendly yet powerful for advanced users
Key Features of Matplotlib
-
Multiple plot types (line, bar, scatter, histogram, pie, etc.)
-
Custom colors, fonts, labels, and styles
-
Support for subplots
-
Integration with Jupyter Notebook
-
Export charts to PNG, PDF, SVG, and more
Installing Matplotlib
You can install Matplotlib using pip:
pip install matplotlibImporting Matplotlib
The commonly used module is pyplot.
import matplotlib.pyplot as pltplt is the standard alias used by most Python developers.
First Simple Plot
import matplotlib.pyplot as plt x = [1, 2, 3, 4] y = [10, 20, 25, 30] plt.plot(x, y) plt.show() # Output: # A simple line graph displaying points connected by linesThis example creates a basic line chart where:
-
xrepresents the horizontal axis -
yrepresents the vertical axis
Common Types of Plots
| Plot Type | Purpose |
|---|---|
| Line Plot | Show trends over time |
| Bar Chart | Compare categories |
| Scatter Plot | Show relationships |
| Histogram | Display distribution |
| Pie Chart | Show proportions |