Matplotlib in Python
Matplotlib is one of the most popular and widely used data visualization libraries in Python.
It provides tools for creating static, animated, and interactive plots in Python. Matplotlib is
especially known for its ability to produce publication-quality figures in a variety of formats
and interactive environments across platforms.
🧩 Key Features
2D Plotting: Line plots, bar charts, histograms, scatter plots, pie charts, etc.
Highly customizable: Colors, labels, axes, legends, and figure size can be customized
extensively.
Multiple output formats: Supports output to PNG, PDF, SVG, EPS, and interactive GUI
backends.
Integration: Works well with NumPy, pandas, and integrates easily with Jupyter
Notebooks.
🔧 Installation
To install Matplotlib, you can use pip:
bash
CopyEdit
pip install matplotlib
📊 Basic Example
Here's a simple line plot example:
python
CopyEdit
import [Link] as plt
# Data
x = [1, 2, 3, 4, 5]
y = [2, 4, 1, 8, 7]
# Create plot
[Link](x, y, label="Line 1", color='blue', marker='o')
# Add title and labels
[Link]("Simple Line Plot")
[Link]("X-axis")
[Link]("Y-axis")
# Show legend
[Link]()
# Display the plot
[Link]()
📈 Common Plot Types
Plot Type Function Used
Line Plot [Link]()
Bar Chart [Link]()
Histogram [Link]()
Scatter Plot [Link]()
Pie Chart [Link]()
Box Plot [Link]()
🖼 Subplots and Layouts
You can create multiple plots in a single figure using [Link]():
python
CopyEdit
[Link](1, 2, 1) # 1 row, 2 columns, 1st plot
[Link]([1, 2, 3], [4, 5, 6])
[Link]("First Plot")
[Link](1, 2, 2) # 2nd plot
[Link]([1, 2, 3], [6, 5, 4])
[Link]("Second Plot")
plt.tight_layout()
[Link]()
🛠 Advanced Features
Object-Oriented API: For more control, you can use the Figure and Axes objects.
Animations: Create animated plots using [Link].
3D Plotting: Available via mpl_toolkits.mplot3d.
Styling: Themes and styles can be changed using [Link]().
Example:
python
CopyEdit
[Link]('ggplot')