👻 Check our latest review to choose the best laptop for Machine Learning engineers and Deep learning tasks!

So we have the following plot:
fig,ax = plt.subplots(5,2,sharex=True,sharey=True,figsize=fig_size)
...and now we would like to give this graph common labels for the x-axis and y-axis. By "common" we mean that there should be a large label for the x-axis below the entire grid of subplots and a large label for the y-axis to the right. Basically you can't find anything about this in the plt.subplots documentation, and various Internet searches suggest that you need to create a large plt.subplot(111) to start with - but how can we then insert my 5*2 subplots into this using plt.subplots?
Answer
This looks like what you actually want. It applies the same approach of another answer to your specific case:
import matplotlib.pyplot as plt fig, ax = plt.subplots(nrows=3, ncols=3, sharex=True, sharey=True, figsize=(6, 6)) fig.text(0.5, 0.04, ’common X’, ha=’center’) fig.text(0.04, 0.5, ’common Y’, va=’center’, rotation=’vertical’)Since I consider it relevant and elegant enough (no need to specify coordinates to place text), I copy (with a slight adaptation) an answer to another related question.
import matplotlib.pyplot as plt fig, axes = plt.subplots(5, 2, sharex=True, sharey=True, figsize=(6,15)) # add a big axis, hide frame fig.add_subplot(111, frameon=False) # hide tick and tick label of the big axis plt.tick_params(labelcolor=’none’, which=’both’, top=False, bottom=False, left=False, right=False) plt.xlabel("common X") plt.ylabel("common Y")subplot xlabel
The xlabel() function in pyplot module of matplotlib library is used to set the label for the x-axis.
Syntax: matplotlib.pyplot.xlabel(xlabel, fontdict=None, labelpad=None, **kwargs)
Parameters: This method accept the following parameters that are described below:
- xlabel: This parameter is the label text. And contains the string value.
- labelpad: This parameter is used for spacing in points from the axes bounding box including ticks and tick labels and its default value is None.
- **kwargs: This parameter is Text properties that is used to control the appearance of the labels.
Example 1
# Implementation of matplotlib.pyplot.xlabels() # function import numpy as np import matplotlib.pyplot as plt t = np.arange(-180.0, 180.0, 0.1) s = np.radians(t)/2. plt.plot(t, s, ’-’, lw = 2) plt.xlabel(’Longitude’) plt.ylabel(’Latitude’) plt.title(’xlabels() function’) plt.grid(True) plt.show()
Example 2
# Implementation of matplotlib.pyplot.xlabels() # function import numpy as np import matplotlib.pyplot as plt valx1 = np.linspace(0.0, 5.0) x2 = np.linspace(0.0, 2.0) valy1 = np.cos(2 * np.pi * valx1) * np.exp(-valx1) y2 = np.cos(2 * np.pi * x2) plt.subplot(2, 1, 1) plt.plot(valx1, valy1, ’o-’) plt.title(’xlabel() Example’) plt.ylabel(’Damped oscillation’) plt.subplot(2, 1, 2) plt.plot(x2, y2, ’.-’) plt.xlabel(’time (s)’) plt.ylabel(’Undamped’) plt.show()
Setting up the graphs. Text elements of the graph
Text elements of a graph
In terms of text content, the following components are distinguished when drawing a chart:
- field title (title);
- figure title (suptitle);
- axes captions (xlabel, ylabel);
- test block on the chart's field (text) or on the figure (figtext);
- annotate - text and pointer.
Each item that contains text besides specific parameters responsible for its setting has parameters of class Text, which give access to a rather large number of settings of appearance and location of the text item. A more detailed description of parameters available from the Text class will be given in a later tutorial.
Below is the code that displays all of the above text elements.
plt.figure(figsize=(10,4))
plt.figtext(0.5, -0.1, "figtext")
plt.suptitle("suptitle")
plt.subplot(121)
plt.title("title")
plt.xlabel("xlabel")
plt.ylabel("ylabel")
plt.text(0.2, 0.2, "text")
plt.annotate("annotate", xy=(0.2, 0.4), xytext=(0.6, 0.7),
arrowprops=dict(facecolor='black', shrink=0.05))
plt.subplot(122)
plt.title("title")
plt.xlabel("xlabel")
plt.ylabel("ylabel")
plt.text(0.5, 0.5, "text")

Graphics items that contain text have a number of configuration parameters that are defined as **kwargs in the official documentation. These are properties of the matplotlib.text.Text class used to control the text representation.
Chart axis labels
If you work with pyplot, the labelx() and labely() functions are used to set the axis labels. When working with an Axes object, the functions set_xlabel() and set_ylabel() are suitable for this purpose.
The main arguments of the functions are almost identical to those described in the title() function.
Annotation
The Annotation tool allows you to set up a text box with specified content and an arrow for a specific location on a graph. Annotation is a powerful tool, let's dwell on it in more detail.
Below is a code sample that demonstrates a simple use of annotation():
import math
x = list(range(-5, 6))
y = [i**2 for i in x]
plt.annotate('min', xy=(0, 0), xycoords='data',
xytext=(0, 10), textcoords='data',
arrowprops=dict(facecolor='g'))
plt.plot(x, y)
👻 Read also: what is the best laptop for engineering students?
We hope this article has helped you to resolve the problem. Apart from Common xlabel/ylabel for matplotlib subplots, check other __main__ Python module-related topics.
Want to excel in Python? See our review of the best Python online courses 2023. If you are interested in Data Science, check also how to learn programming in R.
By the way, this material is also available in other languages:
- Italiano Common xlabel/ylabel for matplotlib subplots
- Deutsch Common xlabel/ylabel for matplotlib subplots
- Français Common xlabel/ylabel for matplotlib subplots
- Español Common xlabel/ylabel for matplotlib subplots
- Türk Common xlabel/ylabel for matplotlib subplots
- Русский Common xlabel/ylabel for matplotlib subplots
- Português Common xlabel/ylabel for matplotlib subplots
- Polski Common xlabel/ylabel for matplotlib subplots
- Nederlandse Common xlabel/ylabel for matplotlib subplots
- 中文 Common xlabel/ylabel for matplotlib subplots
- 한국어 Common xlabel/ylabel for matplotlib subplots
- 日本語 Common xlabel/ylabel for matplotlib subplots
- हिन्दी Common xlabel/ylabel for matplotlib subplots
Tallinn | 2023-01-28
os Python module is always a bit confusing 😭 Common xlabel/ylabel for matplotlib subplots is not the only problem I encountered. Checked yesterday, it works!
New York | 2023-01-28
UI PHP module is always a bit confusing 😭 Common xlabel/ylabel for matplotlib subplots is not the only problem I encountered. Checked yesterday, it works!
San Francisco | 2023-01-28
string Python module is always a bit confusing 😭 Common xlabel/ylabel for matplotlib subplots is not the only problem I encountered. Will use it in my bachelor thesis