Header Ads Widget

⚡ Premium Tools Hub • EXE Apps + Full Python Source Code
Lite • Pro • Bundle Packs • Instant Download

NumPy Matplotlib Tutorial – Data Visualization in Python with Examples

NumPy Matplotlib

Data visualization is one of the most important parts of data science.

While NumPy is used for numerical computation, Matplotlib is used to visualize data in graphical form.

Together, they form a powerful combination for:

  • Data analysis
  • Machine learning
  • Scientific computing
  • Business reporting

What is Matplotlib?

Matplotlib is a Python library used to create:

  • Line plots
  • Bar charts
  • Scatter plots
  • Histograms
  • Pie charts

When combined with NumPy, it becomes very powerful for handling large datasets.


Why Use NumPy with Matplotlib?

  • Fast numerical operations (NumPy)
  • Easy visualization (Matplotlib)
  • Real-time data plotting
  • Used in AI and ML workflows
  • Simplifies data analysis

1. Installing Matplotlib

pip install matplotlib

2. Basic Line Plot

import numpy as np
import matplotlib.pyplot as plt

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()

3. Line Plot with NumPy Range

import numpy as np
import matplotlib.pyplot as plt

x = np.arange(0, 10, 1)
y = x ** 2

plt.plot(x, y)
plt.title("Square Function Plot")
plt.show()

4. Scatter Plot

import numpy as np
import matplotlib.pyplot as plt

x = np.random.randint(1, 50, 10)
y = np.random.randint(1, 50, 10)

plt.scatter(x, y)
plt.title("Scatter Plot Example")
plt.show()

5. Bar Chart

import numpy as np
import matplotlib.pyplot as plt

x = np.array(["A", "B", "C", "D"])
y = np.array([10, 20, 15, 25])

plt.bar(x, y)
plt.title("Bar Chart Example")
plt.show()

6. Histogram

import numpy as np
import matplotlib.pyplot as plt

data = np.random.randn(1000)

plt.hist(data, bins=30)
plt.title("Histogram Example")
plt.show()

7. Pie Chart

import numpy as np
import matplotlib.pyplot as plt

labels = np.array(["Python", "Java", "C++", "JavaScript"])
sizes = np.array([40, 25, 20, 15])

plt.pie(sizes, labels=labels, autopct="%1.1f%%")
plt.title("Pie Chart Example")
plt.show()

8. Multiple Plots

import numpy as np
import matplotlib.pyplot as plt

x = np.arange(0, 5, 1)

plt.plot(x, x)
plt.plot(x, x**2)
plt.plot(x, x**3)

plt.title("Multiple Line Plots")
plt.show()

9. NumPy + Matplotlib Workflow

NumPy → Data Creation & Processing  
Matplotlib → Data Visualization

10. Real-World Example: Sales Data Visualization

import numpy as np
import matplotlib.pyplot as plt

months = np.array(["Jan", "Feb", "Mar", "Apr"])
sales = np.array([200, 300, 250, 400])

plt.plot(months, sales, marker='o')
plt.title("Monthly Sales Report")
plt.show()

11. Real-World Example: Stock Prices

import numpy as np
import matplotlib.pyplot as plt

days = np.arange(1, 11)
price = np.random.randint(100, 200, 10)

plt.plot(days, price)
plt.title("Stock Price Movement")
plt.show()

Common Plot Types

Plot TypePurpose
Line Plot                 Trends over time
Scatter Plot                 Relationship between variables
Bar Chart                 Comparison
Histogram                 Data distribution
Pie Chart                 Percentage distribution

Advantages of NumPy + Matplotlib

  • Fast data processing
  • Easy visualization
  • Powerful for analysis
  • Essential for ML and AI
  • Works with large datasets

Real-World Applications

  • Data science dashboards
  • Financial analysis
  • Machine learning visualization
  • Scientific experiments
  • Business reporting
  • AI model evaluation

Summary

NumPy and Matplotlib together form a powerful data analysis and visualization toolkit. NumPy handles numerical data efficiently, while Matplotlib transforms it into meaningful graphs and charts.

Both are essential tools in NumPy and visualization workflows built with Python.


Conclusion

Learning NumPy with Matplotlib is essential for anyone in data science or machine learning. It allows you to process data efficiently and visualize insights clearly using simple Python code.




Post a Comment

0 Comments