Line Plots in Matplotlib
Line plots are one of the most commonly used visualizations to represent data trends over a continuous range. They are perfect for time-series data, mathematical functions, or any dataset requiring continuity.
Creating Line Plots
To create a line plot, use the plot()
function in Matplotlib. You can pass x and y values as lists or NumPy arrays.
Example: Basic Line Plot
import matplotlib.pyplot as plt
import numpy as np
# Data
x = np.linspace(0, 10, 100) # 100 evenly spaced points between 0 and 10
y = np.sin(x)
# Create line plot
plt.plot(x, y)
# Add title and labels
plt.title("Basic Line Plot")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
# Display the plot
plt.show()
Customizing Line Styles, Colors, and Markers
Matplotlib provides various parameters to style line plots, such as:
Parameter | Description | Example Value |
---|---|---|
color | Line color | 'blue' |
linewidth | Line width | 2.5 |
linestyle | Style of the line ('-' , '--' , ':' , etc.) | '--' |
marker | Marker style for data points | 'o' , 's' |
markersize | Size of the marker | 8 |
markeredgecolor | Color of the marker’s edge | 'black' |
markerfacecolor | Fill color of the marker | 'red' |
Example: Customized Line Plot
# Data
x = np.linspace(0, 10, 10)
y = np.cos(x)
# Create customized line plot
plt.plot(x, y, color='red', linewidth=2, linestyle='--', marker='o', markersize=8, markeredgecolor='black', markerfacecolor='yellow')
# Add title and labels
plt.title("Customized Line Plot")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
# Display the plot
plt.show()
Adding Titles, Labels, and Legends
Titles, axis labels, and legends are essential for understanding a plot.
Example: Line Plot with Titles and Legends
# Data
x = np.linspace(0, 10, 100)
y1 = np.sin(x)
y2 = np.cos(x)
# Create line plots
plt.plot(x, y1, label="Sine", color='blue')
plt.plot(x, y2, label="Cosine", color='green')
# Add title, labels, and legend
plt.title("Sine and Cosine Waves")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.legend()
# Display the plot
plt.show()
Practical Examples
Example 1: Population Growth
# Data
years = [2000, 2005, 2010, 2015, 2020]
population = [2.5, 3.0, 3.5, 4.0, 4.5] # In billions
# Create line plot
plt.plot(years, population, marker='o', color='purple')
# Add title and labels
plt.title("World Population Growth")
plt.xlabel("Year")
plt.ylabel("Population (Billions)")
# Display the plot
plt.show()
Example 2: Temperature Variation
# Data
months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
temp = [5, 7, 10, 15, 20, 25, 30, 28, 22, 16, 10, 6] # In Celsius
# Create line plot
plt.plot(months, temp, color='orange', marker='s')
# Add title and labels
plt.title("Monthly Temperature Variation")
plt.xlabel("Months")
plt.ylabel("Temperature (°C)")
# Display the plot
plt.show()
Try It Yourself
Problem 1: Plot a Quadratic Function
Create a line plot for the function y = x^2
for x
values ranging from -10 to 10.
Show Code
# Data
x = np.linspace(-10, 10, 100)
y = x**2
# Create line plot
plt.plot(x, y, color='blue')
# Add title and labels
plt.title("Quadratic Function")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
# Display the plot
plt.show()
Problem 2: Compare Two Stocks
Plot the stock prices of Company A and Company B over 5 years.
Show Code
# Data
years = ["2018", "2019", "2020", "2021", "2022"]
company_a = [100, 150, 200, 250, 300]
company_b = [80, 120, 180, 220, 280]
# Create line plots
plt.plot(years, company_a, label="Company A", marker='o', color='blue')
plt.plot(years, company_b, label="Company B", marker='s', color='red')
# Add title, labels, and legend
plt.title("Stock Price Comparison")
plt.xlabel("Year")
plt.ylabel("Stock Price")
plt.legend()
# Display the plot
plt.show()
Line plots are versatile and widely used for visualizing trends and relationships in data. Experiment with various customizations to create compelling and informative visualizations.