NumPy – Matplotlib
Data is powerful only when we can see and understand it visually.
That’s where Matplotlib comes in.
When combined with NumPy, it becomes a powerful tool for:
- Data visualization
- Scientific computing
- Machine learning analysis
- Statistical plotting
What is Matplotlib?
Matplotlib is a Python library used to create:
- Line plots
- Bar charts
- Scatter plots
- Histograms
- Scientific graphs
Why Use NumPy with Matplotlib?
NumPy provides fast numerical arrays, while Matplotlib converts those arrays into visual graphs.
Import Libraries
import numpy as np
import matplotlib.pyplot as plt
1. Basic Line Plot
x = np.array([1, 2, 3, 4, 5])
y = np.array([2, 4, 6, 8, 10])
plt.plot(x, y)
plt.title("Simple Line Plot")
plt.xlabel("X Axis")
plt.ylabel("Y Axis")
plt.show()
Output:
- Straight line graph
- Shows linear relationship
2. Plot Using NumPy Generated Data
x = np.linspace(0, 10, 100)
y = np.sin(x)
plt.plot(x, y)
plt.title("Sine Wave")
plt.show()
What this shows:
- Smooth sine wave
- Continuous data visualization
3. Scatter Plot
x = np.random.default_rng().random(50)
y = np.random.default_rng().random(50)
plt.scatter(x, y)
plt.title("Scatter Plot")
plt.show()
Use case:
- Relationship between variables
- Data distribution
4. Histogram (Distribution Visualization)
data = np.random.default_rng().normal(0, 1, 1000)
plt.hist(data, bins=30)
plt.title("Histogram")
plt.show()
Meaning:
- Shows frequency distribution
- Useful in statistics
5. Multiple Plots (Comparison)
x = np.linspace(0, 10, 100)
plt.plot(x, np.sin(x))
plt.plot(x, np.cos(x))
plt.title("Sine vs Cosine")
plt.legend(["sin", "cos"])
plt.show()
Real-World Applications
1. Data Science
- Data visualization
- Pattern recognition
2. Machine Learning
- Feature analysis
- Model performance plots
3. Finance
- Stock trend analysis
- Risk visualization
4. Engineering
- Signal processing
- System modeling
Types of Plots You Can Create
- Line plots
- Scatter plots
- Bar charts
- Histograms
- Heatmaps
Why Use NumPy + Matplotlib?
Using NumPy and Matplotlib provides:
- Fast data processing
- Powerful visualization tools
- Easy integration
- Flexible plotting system
Summary
Matplotlib turns NumPy data into meaningful visual insights using:
plt.plot(), plt.scatter(), plt.hist()
Conclusion
NumPy and Matplotlib together form the foundation of data visualization in Python. They are essential tools for data science, machine learning, and analytics.


0 Comments