• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Data Viz with Python and R

Learn to Make Plots in Python and R

  • Home
  • Seaborn
  • Matplotlib
  • ggplot2
  • Altair
  • About
    • Privacy Policy
  • Visualizing Activation Functions in Neural Networks
  • Confusion Matrix Calculator
  • Visualizing Dropout Rate in Neural Network
  • Visualizing Loss Functions in Neural Networks
  • Show Search
Hide Search

How To Adjust Positions of Axis Labels in Matplotlib?

datavizpyr · September 22, 2020 ·

In this post, we will learn how to adjust positions of x-axis and y-axis labels in Matplotlib in Python. By default, plots with matplotlib places the axis labels in the middle. With matplotlib version 3.3.0, the matplotlib functions set_xlabel and set_ylabel have a new parameter “loc” that can help adjust the positions of axis labels.

For the x-axis label, it supports the values ‘left’, ‘center’, or ‘right’ to place the label towards left/center/right. Similarly, for the y-axis label, it supports the values ‘bottom’, ‘center’, or ‘top’.

Let us get started by loading the libraries needed. Here we need Matplotlib version 3.3. and above to change the position of axis labels.

import matplotlib.pyplot as plt
import numpy as np

We will simulate some data to make a simple histogram using Numpy’s random module
.

# set seed for reproducing
np.random.seed(42)
n = 5000
mean_mu1 = 60
sd_sigma1 = 15
data = np.random.normal(mean_mu1, sd_sigma1, n)

We have the data ready to make a plot with matplotlib with default options. We make a histogram and use set_xlabel and set_ylabel to adjust the axes labels’ font size, weigth and text. This helps us make the histogram with clearly legible labels on both x and y-axes.

fig, ax = plt.subplots() 
ax.hist(data, bins=100, alpha=0.5, 
       density=False) 
ax.set_xlabel("Data",
             fontweight ='bold',
             fontsize=16)
ax.set_ylabel("Count",
               fontweight ='bold',
               fontsize=16)

We can see that by default, matplotlib places the axes label text in the middle of the axes lines.

Matplotlib with default axes label position
Matplotlib with default axes label position

How to Change Axes Label Positions with “loc”?

Let us see an example on how to use set_xlabel and set_ylabel with loc parameter to adjust the positions of axis labels. In the example below, we place x-axis label towards right side and the y-label on top.

fig, ax = plt.subplots() 
ax.hist(data,bins=100, alpha=0.5, 
             density=False) 
ax.set_xlabel("Data",
              fontweight ='bold',
              fontsize=16,
              loc="right")
ax.set_ylabel("Count",
              fontweight ='bold',
              fontsize=16,
              loc="top")
How To Adjust Axes Label Position in Matplotlib?
How To Adjust Axes Label Position in Matplotlib?

Here is another example of placing the axes labels using loc parameter in set_xlabel and set_ylabel functions. In the example below, we place x-axis label towards left side and the y-label at the bottom.

fig, ax = plt.subplots() 
ax.hist(data, bins=100, alpha=0.5,
        density=False) 
ax.set_xlabel("Data",
               fontweight ='bold',
               fontsize=16,
               loc="left")
ax.set_ylabel("Count",
               fontweight ='bold',
               fontsize=16,
               loc="bottom")
Adjust axes label position with loc argument in Matplotlib
Adjust axes label position with loc argument in Matplotlib

Related posts:

Multiple rectangles on the same Matplotlib plotMatplotlib Draw Rectangle: Add, Fill, Annotate & Highlight Regions (Step-by-Step) Connect paired data points in a scatter plot with arrowheads and labelsConnect Paired Data Points in a Scatter Plot in Python (Step-by-Step Guide) Matplotlib Scatter plot with legendHow to Add Legend to Scatterplot Colored by a Variable with Matplotlib in Python Barplot with dollar sign on axis ticksHow To Add Dollar Sign on Axis Ticks in Matplotlib

Filed Under: Matplotlib, Matplotlib set_xlabel, Python Tagged With: Matplotlib, Python

Primary Sidebar

Python & R Viz Hubs

  • Seaborn Guide & Cookbook
  • ggplot2 Guide & Cookbook
  • Matplotlib Guide & Cookbook
  • Confusion Matrix Calculator
  • Visualizing Activation Functions
  • Visualizing Dropout
  • Visualizing Loss Functions

Buy Me a Coffee

Copyright © 2026 · Daily Dish Pro on Genesis Framework · WordPress · Log in

Go to mobile version