In this tutorial, we will learn how we can add axis labels to a Matplotlib graph plot. So to do this, we will use the same plot we had got from our previous article. As this plot already has lines drawn along x and y axis, we will now add labels to its axes.
Before we learn how to add axis labels in Matplotlib, let us first understand its benefits. Why do we need axes labels in our plot in the first place?
Why Do We Want To Add Axis Labels In Matplotlib Plots?
When we draw our plots using Matplotlib, we are passing a set of data to it. So Matplotlib is using this data set as its input to draw the plot. Hence, the output plot that it is drawing is in relation to this input data. We can check this by looking at our Matplotlib graph from previous tutorial.
Understanding our previous tutorial’s Matplotlib plot
We had got the above plot using this Python code:
import matplotlib.pyplot as plt x = range(1, 10) plt.plot(x, [xi*1 for xi in x]) plt.plot(x, [xi*2 for xi in x]) plt.plot(x, [xi*3 for xi in x]) plt.grid() plt.axis([0, 20, 0 , 40]) plt.show()
So let us now compare both the code and its output plot shown above. When we look at the plot, what we are seeing is that it has 3 lines drawn in a 2-Dimensional space. But this 2-Dimensional space will have two axis. Its x-axis and its y-axis. From the plot, we can see that it’s x-axis values are ranging between 0 and 20. But on the other hand, its y-axis values are between 0 and 40.
But what are these values? How did we get these values?
When we take a look at our code, we are seeing that x is a value ranging between 1 & 10. But on the other hand, our program is calclulating the y-axis values for each line.
For the first line, we get the y-axis values by multiplying the x-axis value by 1. But in the second line, we get our y-axis values by multiplying the x-axis value by 2. Similarly, we get the values of our third line by multiplying the x-axis value by 3.
Now, we know how we got the y-values. But we still don’t know what the x and y values represent. What are their measuring? What are its units?
Now if we gave this Matplotlib plot to someone else, even they will not know what x-axis and y-axis values are, isn’t it?
Therefore we need a way to describe these values in our plot. We need a way to mention what x-axis and y-axis values stands for. And we can do this by adding labels to our axes.
Functions To Add Axis Labels In Matplotlib
Lucky for us, adding labels to axes in Matplotlib is very easy. The library has these two useful functions that does exactly this.
plt.xlabel() – This is a Matplotlib function we can use to add label to the x-axis of our plot. A label is simply a string of text. So we can pass this label as a parameter to this function and call it. And as a result of this, the Matplotlib’s output plot will now have the label written along it’s x-axis.
plt.ylabel() – Just like the previous function, this is a Matplotlib function we can use to add label to the y-axis of our plot. Here too, we will pass the label as a parameter to this function and call it. And as a result of this, the Matplotlib’s output plot will this time have the label written along it’s y-axis.
So if we simply call the above two functions with our label strings, we can get those labels added into our output plot.
So with this background, let us change the above code to add our labels to both x & y axis of our output plot. The code will thus looks like this:
import matplotlib.pyplot as plt x = range(1, 10) plt.plot(x, [xi*1 for xi in x]) plt.plot(x, [xi*2 for xi in x]) plt.plot(x, [xi*3 for xi in x]) plt.grid() plt.axis([0, 20, 0 , 40]) plt.xlabel('This is the X axis label') plt.ylabel('This is the Y axis label') plt.show()
From the above code we can see that we have called our xlabel and ylabel functions with labels right after we have drawn our axes. The label we added to our x-axis reads:
This is the X axis label
and for our y-axis:
This is the Y axis label
So as a result of this code, our final Matplotlib plot output looks like this
So from the above plot, we can see that we have successfully added our labels to the plot.
So that is it for this tutorial. We just saw that adding labels to our Matplotlib output plots are pretty straight forward. We just need to call its given function with appropriate labels and it should work. If you still have any questions, do comment below and I will try to help you out. So until next time, ciao! 🙂