Line Graphs in Matplotlib (Python)

Hey coder! Ever wanted to plot how your progress, your followers, or even your mood changes over time? Line graphs are your best buds for that — smooth, simple, and so visual.

In this guide, we’re gonna chill with Matplotlib — a super handy Python library — and learn how to draw clean, easy-to-understand line charts. Perfect if you’re just starting out or want to spice up your data game .

When Should You Use a Line Graph?

Use a line graph when you want to **show change over time** — like how something increases, decreases, or trends. Think of it as a time-traveling chart: you’re looking at what happened yesterday, today, and where things might be going tomorrow.

Here are some classic examples:

Basically, anytime you have something that moves through **time** — a line graph can help you make sense of it.

What You'll Learn

By the end of this quick and cozy tutorial, you'll know how to:

This is the kind of stuff that turns a boring list of numbers into a beautiful, storytelling graph. So get ready to bring your data to life!

Step-by-Step Code Example

Here’s a simple example of how to create a line graph in Matplotlib:

Python

import matplotlib.pyplot as plt

# Sample data
Category = ['A', 'B', 'C', 'D', 'E', 'F']
Values = [10, 20, 15, 25, 10, 30]

# Create the plot
plt.figure(figsize=(8, 5))  # Adjust the size of the plot
plt.plot(Category, Values, 
         color='#1f77b4',         # A soft blue line
         marker='o',              # Circular markers
         linestyle='-',           # Solid line
         linewidth=2, 
         markersize=8)

# Add labels and title
plt.xlabel("Category", fontsize=12)
plt.ylabel("Values", fontsize=12)
plt.title("Basic Line Graph Example", fontsize=14, fontweight='bold')

# Add grid for better readability
plt.grid(True, linestyle='--', alpha=0.5)

# Display the plot
plt.tight_layout()
plt.show()

        

Understanding the Code Step-by-Step



What is Linestyle?

Linestyle controls how the line appears between data points — solid, dashed, or dotted.



What is a Marker?

Marker highlights each data point on the graph — like dots, stars, or squares.



Marker Size & Line Width

Want to make your graph pop a bit more? That's where markersize and linewidth come in!

Just add them like this:
plt.plot(x, y, marker='o', markersize=10, linewidth=2)

Here's the output :








🔹Mini Project: Track Your Screen Time!

Wanna try out your new matplotlib skills? Here's a chill project idea:

Track your screen time for a week (be honest), then plot it using a line graph!

This is a cool way to visualize your habits—and maybe get that little reality check we all need.

Bonus points if you color-code it or add a title like “How Much I Scrolled This Week”




More educational content :