1. Creating a Simple Line Chart
When it comes to data, line charts are like your trusty notebook where you jot down important life dates. They consist of a set of points connected by a line, showing how values change over time or based on other variables.
Alright, let’s dive into a basic example to see how a line chart is created in Matplotlib.
import matplotlib.pyplot as plt
# Data for the chart
x = [1, 2, 3, 4, 5]
y = [2, 3, 5, 7, 11]
# Creating the line chart
plt.plot(x, y)
# Displaying the chart
plt.show()
This simple bit of code will create a line chart showing how each element in y
depends on the corresponding element in x
. No big deal – just pass lists of values, and Matplotlib takes care of the rest!

2. Customizing Charts
Now that you’ve created your first chart, it’s time to spice it up! Because, as we know, first impressions matter – even for charts.
Customizing Line Colors and Styles
Sometimes, to emphasize information, we need colors and different line styles. Let’s add a little flair:
plt.plot(x, y, color='blue', linestyle='--', marker='o')
Here, color
, linestyle
, and marker
let you set the color, line style (e.g., dashed), and marker type (e.g., circles) respectively.

Adding Titles and Labels
Imagine your chart without any titles or labels – it’s just a fancy squiggle. So let’s give it a title and add axis labels:
plt.title("Example Line Chart")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
Be honest with yourself: always add titles and labels to your charts. It makes them way easier to understand – even for yourself (unless you’ve memorized what each line represents).

Legend on the Chart
If your chart has multiple lines, always add a legend so no one gets confused.
plt.plot(x, y, color='blue', linestyle='--', marker='o', label='Line 1')
plt.plot(x2, y2, color='red', linestyle='--', marker='X', label='Line 2')
plt.legend()
After setting it up, your audience will totally be impressed by your professionalism.

3. Practice Task
Let’s sharpen your skills by creating a line chart with some inspiring data. Imagine you have temperature data for a week:
days = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
temperatures = [20, 22, 23, 21, 24, 25, 26]
plt.plot(days, temperatures, color='green', linestyle='-', marker='s', label='Temperature')
plt.title("Temperature Change Over the Week")
plt.xlabel("Days of the Week")
plt.ylabel("Temperature, °C")
plt.legend()
plt.grid(True)
plt.show()
In this example, we added square markers and a main line. Plus, we turned on the grid with plt.grid(True)
, making the data easier to read.

Oh no! Something went wrong...
Yeah, it happens. If your chart looks weird, check the following:
- Make sure the lengths of
x
andy
lists match. If the lengths don’t match – nothing works. - Double-check your syntax: typos in function names or parameters can ruin the whole chart.
Remember, visualization isn’t just about looks – it’s about effectiveness. Charts should be useful and informative. Every time you make a chart, ask yourself: "Does it help reveal something new in the data?"
So, we’ve just explored how to create and customize a basic line chart with Matplotlib. Now, armed with this powerful tool, you can visualize tons of data with style and grace. On to the next lecture to learn how to make your charts even more impressive!
GO TO FULL VERSION