Lesson 32 of 60 · python
Line, Scatter, and Bar Charts
Duration: 15 minutes
Common Plot Types
We'll explore the most frequently used chart types.
Line chart (trend over time)
import pandas as pd
import matplotlib.pyplot as plt
time = pd.date_range(start='2024-01-01', periods=12, freq='M')
values = np.random.randn(12).cumsum()
plt.plot(time, values, marker='o')
plt.title('Monthly Trend')
plt.xlabel('Month')
plt.ylabel('Cumulative Value')
plt.grid(True)
plt.show()
Scatter plot (relationship between two variables)
x = np.random.rand(100)
y = 2*x + np.random.randn(100)*0.2
plt.scatter(x, y, alpha=0.7)
plt.title('Scatter Plot')
plt.xlabel('Feature X')
plt.ylabel('Target Y')
plt.show()
Bar chart (categorical comparison)
categories = ['A', 'B', 'C', 'D']
counts = [23, 45, 12, 30]
plt.bar(categories, counts, color='skyblue')
plt.title('Bar Chart of Categories')
plt.xlabel('Category')
plt.ylabel('Count')
plt.show()
When to use which?
- Line: trends over ordered axis (time, distance).
- Scatter: explore correlation, detect outliers.
- Bar: compare discrete groups.