#1 Data Analytics Program in India
₹2,499₹1,499Enroll Now
5 min read min read

Line Plots

Learn to create line charts for trends and time series

Line Plots

When to Use Line Plots?

Line plots show change over time:

  • Stock prices over months
  • Temperature over days
  • Sales over years

Basic Line Plot

code.py
import matplotlib.pyplot as plt

months = ['Jan', 'Feb', 'Mar', 'Apr', 'May']
sales = [100, 120, 115, 140, 160]

fig, ax = plt.subplots()
ax.plot(months, sales)
ax.set_title('Monthly Sales')
plt.show()

Add Markers

code.py
ax.plot(months, sales, marker='o')

Common markers:

  • 'o' = circles
  • 's' = squares
  • '^' = triangles
  • 'x' = x marks

Change Line Style

code.py
ax.plot(months, sales, linestyle='--')

Line styles:

  • '-' = solid (default)
  • '--' = dashed
  • ':' = dotted
  • '-.' = dash-dot

Change Color

code.py
ax.plot(months, sales, color='red')

Colors: 'red', 'blue', 'green', 'orange', 'purple', etc.

All Together

code.py
ax.plot(months, sales, marker='o', linestyle='--', color='green')

Shorthand:

code.py
ax.plot(months, sales, 'go--')  # green circles, dashed line

Multiple Lines

code.py
import matplotlib.pyplot as plt

months = ['Jan', 'Feb', 'Mar', 'Apr', 'May']
sales_2023 = [100, 120, 115, 140, 160]
sales_2024 = [110, 130, 125, 155, 180]

fig, ax = plt.subplots()
ax.plot(months, sales_2023, marker='o', label='2023')
ax.plot(months, sales_2024, marker='s', label='2024')

ax.set_title('Sales Comparison')
ax.legend()  # Show legend
plt.show()

Add Grid

code.py
ax.grid(True)

Set Axis Limits

code.py
ax.set_ylim(0, 200)  # Y axis from 0 to 200
ax.set_xlim(0, 4)    # X axis from 0 to 4

Complete Example

code.py
import matplotlib.pyplot as plt

# Data
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun']
revenue = [50, 55, 60, 58, 70, 85]
costs = [40, 42, 45, 44, 50, 55]

# Create chart
fig, ax = plt.subplots(figsize=(10, 6))

ax.plot(months, revenue, marker='o', label='Revenue', color='green')
ax.plot(months, costs, marker='s', label='Costs', color='red')

ax.set_title('Revenue vs Costs')
ax.set_xlabel('Month')
ax.set_ylabel('Amount ($K)')
ax.legend()
ax.grid(True, alpha=0.3)

plt.show()

Key Points

  • plot() creates line charts
  • Use marker for data points
  • Use linestyle for line type
  • Use color to change color
  • Use label + legend() for multiple lines
  • Use grid(True) for grid lines

What's Next?

Learn scatter plots for showing relationships between two variables.