Matplotlib is a powerful Python library used for data visualization. It allows you to create charts, graphs, and plots to represent data visually.
In Data Analytics and Data Science, visualization helps you understand patterns, trends, and insights clearly.
Why Use Matplotlib?
- Create different types of charts
- Customize graphs easily
- Works well with NumPy and Pandas
- Industry-standard visualization library
- Helps present data professionally
Installing Matplotlib
If not installed, use:
pip install matplotlib
Import Matplotlib:
import matplotlib.pyplot as plt
plt is the commonly used alias.
Basic Line Plot
import matplotlib.pyplot as pltx = [1, 2, 3, 4, 5]
y = [10, 20, 25, 30, 40]plt.plot(x, y)
plt.show()
This creates a simple line graph.
Adding Labels and Title
plt.plot(x, y)plt.title("Sales Growth")
plt.xlabel("Months")
plt.ylabel("Revenue")plt.show()
Bar Chart
categories = ["IT", "HR", "Finance"]
values = [50, 30, 40]plt.bar(categories, values)
plt.title("Employees by Department")
plt.show()
Histogram
data = [10, 20, 20, 30, 40, 40, 40, 50]plt.hist(data)
plt.title("Data Distribution")
plt.show()
Scatter Plot
x = [1, 2, 3, 4, 5]
y = [5, 15, 20, 25, 30]plt.scatter(x, y)
plt.title("Scatter Plot Example")
plt.show()
Common Customizations
Add grid:
plt.grid(True)
Change line style:
plt.plot(x, y, linestyle="--")
Add legend:
plt.plot(x, y, label="Sales")
plt.legend()
Why Matplotlib is Important in Data Analytics
Visualization helps you:
Identify trends
Compare categories
Detect outliers
Present insights clearly
Make better business decisions
Key Takeaway
Matplotlib is the foundation of data visualization in Python. Learning how to create and customize charts will help you communicate data insights effectively in real-world analytics projects.