Data Visualization in Python using Matplotlib
Matplotlib is a powerful Python library for creating static, animated, and interactive
visualizations. It helps visualize data in different forms such as line plots, bar charts, pie
charts, scatter plots, and histograms.
To install Matplotlib:
pip install matplotlib
1. Line Plot
Used to show trends or changes over time.
import [Link] as plt
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
[Link](x, y, color="blue", marker="o")
[Link]("Simple Line Plot")
[Link]("X-axis")
[Link]("Y-axis")
[Link]()
2. Bar Chart
Used to compare different categories or groups.
subjects = ["Math", "Science", "English", "History"]
scores = [85, 90, 78, 88]
[Link](subjects, scores, color="green")
[Link]("Student Scores by Subject")
[Link]("Subjects")
[Link]("Scores")
[Link]()
3. Pie Chart
Used to represent proportions or percentages of a whole.
labels = ["Apples", "Bananas", "Cherries", "Dates"]
sizes = [30, 25, 20, 25]
[Link](sizes, labels=labels, autopct="%1.1f%%", startangle=90)
[Link]("Fruit Distribution")
[Link]()
4. Scatter Plot
Used to display relationships or correlations between two variables.
x = [1, 2, 3, 4, 5, 6]
y = [2, 3, 5, 7, 11, 13]
[Link](x, y, color="red")
[Link]("Scatter Plot Example")
[Link]("X Values")
[Link]("Y Values")
[Link]()
5. Histogram
Used to visualize data distribution.
import numpy as np
data = [Link](1000)
[Link](data, bins=20, color="purple", edgecolor="black")
[Link]("Histogram of Random Data")
[Link]("Value")
[Link]("Frequency")
[Link]()
6. Multiple Plots in One Figure
You can combine multiple lines or charts in the same figure.
x = [1, 2, 3, 4, 5]
y1 = [2, 4, 6, 8, 10]
y2 = [1, 3, 5, 7, 9]
[Link](x, y1, label="Even Numbers", color="blue")
[Link](x, y2, label="Odd Numbers", color="orange")
[Link]("Multiple Line Plots")
[Link]("X-axis")
[Link]("Y-axis")
[Link]()
[Link]()
7. Customizing Plots
Matplotlib allows customization of colors, styles, grid lines, and more.
[Link](x, y1, "r--", linewidth=2, marker="o", markersize=6)
[Link]("Customized Line Plot", fontsize=14)
[Link](True)
[Link]()
8. Real-Life Applications
- Data Analysis: Visualizing trends in sales or performance
- Machine Learning: Plotting model accuracy or loss curves
- Scientific Research: Showing experimental results
- Finance: Visualizing stock price trends over time