Line and Bar Charts are two of the most commonly used visualizations in Data Analytics. They help you compare values, identify trends, and present data clearly.
First, import Matplotlib:
import matplotlib.pyplot as plt
1. Line Chart
A Line Chart is used to show trends over time or continuous data.
Example: Monthly Sales
months = ["Jan", "Feb", "Mar", "Apr", "May"]
sales = [10000, 15000, 20000, 18000, 25000]plt.plot(months, sales)
plt.title("Monthly Sales Trend")
plt.xlabel("Months")
plt.ylabel("Sales")
plt.show()
Adding Customization
plt.plot(months, sales, marker="o", linestyle="--", label="Sales")
plt.title("Monthly Sales Trend")
plt.xlabel("Months")
plt.ylabel("Sales")
plt.legend()
plt.grid(True)
plt.show()
When to Use Line Chart:
- Showing growth over time
- Tracking performance
- Analyzing trends
2. Bar Chart
A Bar Chart is used to compare categories.
Example: Employees by Department
departments = ["IT", "HR", "Finance", "Marketing"]
employees = [40, 25, 30, 20]plt.bar(departments, employees)
plt.title("Employees by Department")
plt.xlabel("Department")
plt.ylabel("Number of Employees")
plt.show()
Horizontal Bar Chart
plt.barh(departments, employees)
plt.title("Employees by Department")
plt.show()
When to Use Bar Chart:
- Comparing categories
- Showing differences between groups
- Presenting survey results
Line Chart vs Bar Chart
Line Chart:
- Best for trends
- Continuous data
- Time-based analysis
Bar Chart:
- Best for comparisons
- Categorical data
- Static group comparison
Key Takeaway
Line Charts help visualize trends over time, while Bar Charts are ideal for comparing categories. Both are essential tools for effective data visualization in analytics projects.