Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add subtitles for rows of a plot

For a plot like this:

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 2 * np.pi, 400)
y = np.sin(x ** 2)

f, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, sharey=True)
ax1.plot(x, y)
ax2.scatter(x, y)
ax3.scatter(x, 2 * y ** 2 - 1, color='r')
ax4.plot(x, 2 * y ** 2 - 1, color='r')

how can I add subtitles for a row?. It should look like this:

enter image description here

I made "Title1" and "Title2" with photoshop, how can I add them to the plot in python?

like image 994
spore234 Avatar asked Nov 24 '16 14:11

spore234


People also ask

How do I add subtitles to a plot in Python?

Adding a Subtitle to a Matplotlib Plot suptitle() method. This method acts as a higher-level title. We can then use the regular title as a subtitle.


2 Answers

To make a title for a plot with subplots, matplotlib has pyplot.suptitle. Since there can only be one suptitle per figure it does not solve the problem if you want to have two rows of figures.

Using plt.text() one can set text to an axes, which is also not wanted here, so I would suggest to use plt.figtext

It may then be required to adjust the spacingbetween the rows of the subplots using plt.subplots_adjust(hspace = 0.3 )

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 2 * np.pi, 400)
y = np.sin(x ** 2)

f, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, sharey=True)
ax1.plot(x, y)
ax2.scatter(x, y)
ax3.scatter(x, 2 * y ** 2 - 1, color='r')
ax4.plot(x, 2 * y ** 2 - 1, color='r')

plt.figtext(0.5,0.95, "A tremendously long title that wouldn't fit above a single figure", ha="center", va="top", fontsize=14, color="r")
plt.figtext(0.5,0.5, "Yet another multi-worded title that needs some space", ha="center", va="top", fontsize=14, color="r")
plt.subplots_adjust(hspace = 0.3 )
plt.savefig(__file__+".png")
plt.show()

enter image description here

like image 62
ImportanceOfBeingErnest Avatar answered Oct 02 '22 13:10

ImportanceOfBeingErnest


New in matplotlib 3.4.0

We can now do this with built-in suptitles for subfigures.

First create a 2x1 subfigure grid via Figure.subfigures. Then for each subfigure, give it its own 1x2 subplot grid and suptitle:

fig = plt.figure(constrained_layout=True)
# fig.suptitle('Figure title')            # set global suptitle if desired

(subfig1, subfig2) = fig.subfigures(2, 1) # create 2x1 subfigures
(ax1, ax2) = subfig1.subplots(1, 2)       # create 1x2 subplots on subfig1
(ax3, ax4) = subfig2.subplots(1, 2)       # create 1x2 subplots on subfig2

subfig1.suptitle('Title 1')               # set suptitle for subfig1
ax1.plot(x, y)
ax2.scatter(x, y)

subfig2.suptitle('Title 2', color='r')    # set suptitle for subfig2
ax3.scatter(x, 2 * y ** 2 - 1, color='r')
ax4.plot(x, 2 * y ** 2 - 1, color='r')

Note: it's also possible to put subfigures into an existing plt.subplots grid via Figure.add_subfigure as shown in how to plot subfigures.

suptitled subfigures

like image 22
tdy Avatar answered Oct 02 '22 13:10

tdy