How To Adjust Position of Axis Labels in Matplotlib?
In this article, we will see how to adjust positions of the x-axis and y-axis labels in Matplotlib which is a library for plotting in python language. By default, these labels are placed in the middle, but we can alter these positions using the “loc” parameter in set_xlabel and set_ylabel function of matplotlib.
Note: “loc” parameter is only available in Matplotlib version 3.3.0 onwards.
Let’s understand with step wise:
First, let’s import all the required libraries.
Now we will create fake data using the NumPy library. Here we are using the sample sub-module from the random module to create a dataset of random values.
Now we have created data let’s plot this data using the default options of matplotlib and then start experimenting with its positions. We can clearly see that these labels are in the center by default. The bins parameter tells you the number of bins that your data will be divided into. Matplotlib allows you to adjust the transparency of a graph plot using the alpha attribute. By default, alpha=1
Changing the positions of labels using the loc parameter
Here we will move the y-label to the bottom and x-label to the extreme right using the loc parameter.
Let’s take another example here we will move the y-label to the top.