The Wayback Machine - https://web.archive.org/web/20241009154941/https://www.geeksforgeeks.org/how-to-adjust-position-of-axis-labels-in-matplotlib/
Open In App

How To Adjust Position of Axis Labels in Matplotlib?

Last Updated : 11 Dec, 2020
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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:

Step 1:

First, let’s import all the required libraries.

Python3




import matplotlib.pyplot as plt
import numpy as np


Step 2:

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.

Python3




from random import sample
data = sample(range(1, 1000), 100)


Step 3:

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 

Python3




fig, ax = plt.subplots() 
ax.hist( data, bins = 100, alpha = 0.6) 
ax.set_xlabel("X-Label" , fontsize = 16)
ax.set_ylabel("Y-label" , fontsize = 16)


Output:

The default position of labels

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.

Python3




fig, ax = plt.subplots() 
ax.hist( data, bins = 100, alpha = 0.6) 
ax.set_xlabel("X-Label",
              fontsize = 16, loc = "right")
  
ax.set_ylabel("Y-Label", 
              fontsize = 16, loc = "bottom")


Output:

Y-label to the bottom and X-label to extreme right

Let’s take another example here we will move the y-label to the top.

Python3




fig, ax = plt.subplots() 
ax.hist( data, bins = 100, alpha = 0.6) 
ax.set_xlabel("X-Label", 
              fontsize = 16, loc = "right")
  
ax.set_ylabel("Y-Label", 
              fontsize = 16, loc = "top")


Output:

Y-label to the top and X-label to extreme right



Previous Article
Next Article

Similar Reads

How to Adjust the Position of a Matplotlib Colorbar?
A colorbar is a bar that has various colors in it and is placed along the sides of the Matplotlib chart. It is the legend for colors shown in the chart. By default, the position of the Matplotlib color bar is on the right side. The position of the Matplotlib color bar can be changed according to our choice by using the functions from Matplotlib Axe
5 min read
How to Adjust Title Position in Matplotlib?
In this article, you learn how to modify the Title position in matplotlib in Python. Method 1: Using matplotlib.pyplot.title() function The title() method in matplotlib module is used to specify title of the visualization depicted and displays the title using various attributes. Syntax: matplotlib.pyplot.title(label, fontdict=None, loc='center', pa
3 min read
How to change the size of axis labels in Matplotlib?
Matplotlib is a great data plotting tool. It's used for visualizing data and also for presenting the data to your team on a presentation or for yourself for future reference. So, while presenting it might happen that the "X-label" and "y-label" are not that visible and for that reason, we might want to change its font size. So in this article, we a
2 min read
How to Hide Axis Text Ticks or Tick Labels in Matplotlib?
The Matplotlib library by default shows the axis ticks and tick labels. Sometimes it is necessary to hide these axis ticks and tick labels. This article discusses some methods by which this can be done. Ticks: The axes' points are marked with ticks, which are also known as little geometrical scale lines.Tick labels: They are the name given to the t
4 min read
Rotate axis tick labels in Seaborn and Matplotlib
Seaborn and Matplotlib both are commonly used libraries for data visualization in Python. We can draw various types of plots using Matplotlib like scatter, line, bar, histogram, and many more. On the other hand, Seaborn provides a variety of visualization patterns. It uses easy syntax and has easily interesting default themes. It specializes in sta
3 min read
Matplotlib.axis.Axis.get_minorticklines() function in Python
Matplotlib is a library in Python and it is numerical – mathematical extension for NumPy library. It is an amazing visualization library in Python for 2D plots of arrays and used for working with the broader SciPy stack. matplotlib.axis.Axis.get_minorticklines() Function The Axis.get_minorticklines() function in axis module of matplotlib library is
2 min read
Matplotlib.axis.Axis.set_figure() function in Python
Matplotlib is a library in Python and it is numerical – mathematical extension for NumPy library. It is an amazing visualization library in Python for 2D plots of arrays and used for working with the broader SciPy stack. Matplotlib.axis.Axis.set_figure() Function The Axis.set_figure() function in axis module of matplotlib library is used to set the
2 min read
Matplotlib.axis.Axis.remove_overlapping_locs() function in Python
Matplotlib is a library in Python and it is numerical – mathematical extension for NumPy library. It is an amazing visualization library in Python for 2D plots of arrays and used for working with the broader SciPy stack. Matplotlib.axis.Axis.remove_overlapping_locs Function The Axis.remove_overlapping_locs function in axis module of matplotlib libr
2 min read
Matplotlib.axis.Axis.set_tick_params() in Python
Matplotlib is a library in Python and it is numerical – mathematical extension for NumPy library. It is an amazing visualization library in Python for 2D plots of arrays and used for working with the broader SciPy stack. Matplotlib.axis.Axis.set_tick_params() Function The Axis.set_tick_params() function in axis module of matplotlib library is used
2 min read
Matplotlib.axis.Axis.get_figure() function in Python
Matplotlib is a library in Python and it is numerical – mathematical extension for NumPy library. It is an amazing visualization library in Python for 2D plots of arrays and used for working with the broader SciPy stack. Matplotlib.axis.Axis.get_figure() Function The Axis.get_figure() function in axis module of matplotlib library is used to get the
2 min read
Matplotlib.axis.Axis.cla() function in Python
Matplotlib is a library in Python and it is numerical – mathematical extension for NumPy library. It is an amazing visualization library in Python for 2D plots of arrays and used for working with the broader SciPy stack. matplotlib.axis.Axis.cla() Function The Axis.cla() function in axis module of matplotlib library is used to clear this axis. Synt
2 min read
Matplotlib.axis.Axis.get_major_locator() function in Python
Matplotlib is a library in Python and it is numerical – mathematical extension for NumPy library. It is an amazing visualization library in Python for 2D plots of arrays and used for working with the broader SciPy stack. matplotlib.axis.Axis.get_major_locator() Function The Axis.get_major_locator() function in axis module of matplotlib library is u
1 min read
Matplotlib.axis.Axis.get_major_formatter() function in Python
Matplotlib is a library in Python and it is numerical – mathematical extension for NumPy library. It is an amazing visualization library in Python for 2D plots of arrays and used for working with the broader SciPy stack. matplotlib.axis.Axis.get_major_formatter() Function The Axis.get_major_formatter() function in axis module of matplotlib library
1 min read
Matplotlib.axis.Axis.get_minor_formatter() function in Python
Matplotlib is a library in Python and it is numerical – mathematical extension for NumPy library. It is an amazing visualization library in Python for 2D plots of arrays and used for working with the broader SciPy stack. matplotlib.axis.Axis.get_minor_formatter() Function The Axis.get_minor_formatter() function in axis module of matplotlib library
1 min read
Matplotlib.axis.Axis.get_minor_locator() function in Python
Matplotlib is a library in Python and it is numerical – mathematical extension for NumPy library. It is an amazing visualization library in Python for 2D plots of arrays and used for working with the broader SciPy stack. matplotlib.axis.Axis.get_minor_locator() Function The Axis.get_minor_locator() function in axis module of matplotlib library is u
1 min read
Matplotlib.axis.Axis.set_major_formatter() function in Python
Matplotlib is a library in Python and it is numerical – mathematical extension for NumPy library. It is an amazing visualization library in Python for 2D plots of arrays and used for working with the broader SciPy stack. matplotlib.axis.Axis.set_major_formatter() Function The Axis.set_major_formatter() function in axis module of matplotlib library
2 min read
Matplotlib.axis.Axis.set_minor_formatter() function in Python
Matplotlib is a library in Python and it is numerical – mathematical extension for NumPy library. It is an amazing visualization library in Python for 2D plots of arrays and used for working with the broader SciPy stack. matplotlib.axis.Axis.set_minor_formatter() Function The Axis.set_minor_formatter() function in axis module of matplotlib library
2 min read
Matplotlib.axis.Axis.set_minor_locator() function in Python
Matplotlib is a library in Python and it is numerical – mathematical extension for NumPy library. It is an amazing visualization library in Python for 2D plots of arrays and used for working with the broader SciPy stack. matplotlib.axis.Axis.set_minor_locator() Function The Axis.set_minor_locator() function in axis module of matplotlib library is u
2 min read
Matplotlib.axis.Axis.grid() function in Python
Matplotlib is a library in Python and it is numerical – mathematical extension for NumPy library. It is an amazing visualization library in Python for 2D plots of arrays and used for working with the broader SciPy stack. matplotlib.axis.Axis.grid() Function The Axis.grid() function in axis module of matplotlib library is used to Configure the grid
2 min read
Matplotlib.axis.Axis.get_ticklocs() function in Python
Matplotlib is a library in Python and it is numerical – mathematical extension for NumPy library. It is an amazing visualization library in Python for 2D plots of arrays and used for working with the broader SciPy stack. matplotlib.axis.Axis.get_ticklocs() Function The Axis.get_ticklocs() function in axis module of matplotlib library is used to get
2 min read
Matplotlib.axis.Axis.get_gridlines() function in Python
Matplotlib is a library in Python and it is numerical – mathematical extension for NumPy library. It is an amazing visualization library in Python for 2D plots of arrays and used for working with the broader SciPy stack. matplotlib.axis.Axis.get_gridlines() Function The Axis.get_gridlines() function in axis module of matplotlib library is used to g
2 min read
Matplotlib.axis.Axis.get_ticklines() function in Python
Matplotlib is a library in Python and it is numerical – mathematical extension for NumPy library. It is an amazing visualization library in Python for 2D plots of arrays and used for working with the broader SciPy stack. matplotlib.axis.Axis.get_ticklines() Function The Axis.get_ticklines() function in axis module of matplotlib library is used to g
2 min read
Matplotlib.axis.Axis.get_ticklabels() function in Python
Matplotlib is a library in Python and it is numerical – mathematical extension for NumPy library. It is an amazing visualization library in Python for 2D plots of arrays and used for working with the broader SciPy stack. matplotlib.axis.Axis.get_ticklabels() Function The Axis.get_ticklabels() function in axis module of matplotlib library is used to
2 min read
Matplotlib.axis.Axis.get_minorticklocs() function in Python
Matplotlib is a library in Python and it is numerical – mathematical extension for NumPy library. It is an amazing visualization library in Python for 2D plots of arrays and used for working with the broader SciPy stack. matplotlib.axis.Axis.get_minorticklocs() Function The Axis.get_minorticklocs() function in axis module of matplotlib library is u
2 min read
Matplotlib.axis.Axis.get_minorticklabels() function in Python
Matplotlib is a library in Python and it is numerical – mathematical extension for NumPy library. It is an amazing visualization library in Python for 2D plots of arrays and used for working with the broader SciPy stack. Matplotlib.axis.Axis.get_minorticklabels() Function The Axis.get_minorticklabels() function in axis module of matplotlib library
2 min read
Matplotlib.axis.Axis.get_majorticklines() function in Python
Matplotlib is a library in Python and it is numerical – mathematical extension for NumPy library. It is an amazing visualization library in Python for 2D plots of arrays and used for working with the broader SciPy stack. matplotlib.axis.Axis.get_majorticklines() Function The Axis.get_majorticklines() function in axis module of matplotlib library is
2 min read
Matplotlib.axis.Axis.get_minor_ticks() function in Python
Matplotlib is a library in Python and it is numerical – mathematical extension for NumPy library. It is an amazing visualization library in Python for 2D plots of arrays and used for working with the broader SciPy stack. Matplotlib.axis.Axis.get_minor_ticks() Function The Axis.get_minor_ticks() function in axis module of matplotlib library is used
2 min read
Matplotlib.axis.Axis.get_majorticklocs() function in Python
Matplotlib is a library in Python and it is numerical – mathematical extension for NumPy library. It is an amazing visualization library in Python for 2D plots of arrays and used for working with the broader SciPy stack. matplotlib.axis.Axis.get_majorticklocs() Function The Axis.get_majorticklocs() function in axis module of matplotlib library is u
2 min read
Matplotlib.axis.Axis.get_majorticklabels() function in Python
Matplotlib is a library in Python and it is numerical – mathematical extension for NumPy library. It is an amazing visualization library in Python for 2D plots of arrays and used for working with the broader SciPy stack. Matplotlib.axis.Axis.get_majorticklabels() Function The Axis.get_majorticklabels() function in axis module of matplotlib library
2 min read
Matplotlib.axis.Axis.get_major_ticks() function in Python
Matplotlib is a library in Python and it is numerical – mathematical extension for NumPy library. It is an amazing visualization library in Python for 2D plots of arrays and used for working with the broader SciPy stack. Matplotlib.axis.Axis.get_major_ticks() Function The Axis.get_major_ticks() function in axis module of matplotlib library is used
2 min read