Science Oxford Logo

Add Labels to your Graph


Why add labels?

It can be difficult to work out what is happening on a graph, so adding labels can help people to understand what you want to show them.

You can stop misconceptions by explaining what your graph shows, and if there are any unknowns, so that other people do not come to the wrong conclusion!


Adding a title

Giving your graph a title is a good start!

Scroll down to the bottom of your code, and make a new line before plt.show(). Add this code in there, and edit it to change your title!

plt.title("This is the title of my graph")

Did it work? Make sure your new title show up at the top of your graph!


Labelling your axes

Your graph has two axes - the x-axis is along the bottom (your months) and the y-axis is up the side (your averages).

Use these two lines of code to put labels on each of these axes (put them before plt.show()).

plt.xlabel("x-axis label")
plt.ylabel("y-axis label")

Labelling the line

You can also label the line itself, which can be useful if you put more than one line on your graph!

To do this, you need to edit the line of code which created your graph, and also add a new line of code at the bottom.

Find the line of code you added earlier: plt.plot(months, averages, color="red")

You can add an extra argument inside the brackets to give it a label:

plt.plot(months, averages, color="red", label="this is a label!")

To make this label show up, you also need to add a new line of code at the bottom (before plt.show()):

plt.legend()

Test it out and make sure you don't get any error messages!