1. Line vs Scatter
- Line plot — points connected in sequence; good for smaller series and trends over time
- Scatter plot — isolated markers; good for larger sets and non-linear pairwise relationships
2. Basic Line Plot
import matplotlib.pyplot as plt
days = [1, 2, 3, 4, 5, 6, 7]
sales = [120, 150, 90, 200, 180, 220, 160]
plt.plot(days, sales, marker="o", linewidth=2, alpha=0.8, label="Daily Sales")
plt.xlabel("Days")
plt.ylabel("Sales ($)")
plt.title("Sales Trend Over a Week")
plt.legend()
plt.show()
marker, linewidth, and alpha control shape, line thickness, and transparency.
3. Basic Scatter Plot
temperature = [18, 22, 25, 28, 30, 32]
ice_cream_sales = [40, 55, 70, 95, 110, 130]
plt.scatter(temperature, ice_cream_sales, marker="x", alpha=0.7, label="Sales vs Temperature")
plt.xlabel("Temperature (°C)")
plt.ylabel("Ice Cream Sales ($)")
plt.title("Relationship Between Temperature and Sales")
plt.legend()
plt.show()
4. Real Data: Sample + Monthly Revenue
import pandas as pd
df = pd.read_csv("ecommerce_orders.csv", parse_dates=["Order Date"])
sample_df = df.sample(n=100) # reduce clutter
monthly_revenue = df.set_index("Order Date")["Revenue"].resample("ME").sum()
fig, ax = plt.subplots(figsize=(7, 4))
ax.plot(monthly_revenue.index, monthly_revenue.values, linewidth=2)
ax.set_xlabel("Month")
ax.set_ylabel("Monthly Revenue ($)")
ax.set_title("Monthly Revenue Trend")
plt.show()
fig, ax = plt.subplots(figsize=(6, 4))
ax.scatter(sample_df["Unit Price"], sample_df["Revenue"], alpha=0.5)
ax.set_xlabel("Unit Price ($)")
ax.set_ylabel("Revenue ($)")
ax.set_title("Unit Price vs. Revenue")
plt.show()
Use a line for ordered time series; use scatter when points do not form a single continuous path (e.g. Unit Price vs Revenue).