A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/how-to-add-title-to-subplots-in-matplotlib/ below:

How to Add Title to Subplots in Matplotlib?

How to Add Title to Subplots in Matplotlib?

Last Updated : 26 Nov, 2022

In this article, we will see how to add a title to subplots in Matplotlib? Let's discuss some concepts :

Steps Needed

Example 1: (Using set_title() method)

We use matplotlib.axes._axes.Axes.set_title(label) method to set title (string label) for the current subplot Axes.

Python3
# importing packages
import numpy as np
import matplotlib.pyplot as plt

# create data
x=np.array([1, 2, 3, 4, 5])

# making subplots
fig, ax = plt.subplots(2, 2)

# set data with subplots and plot
ax[0, 0].plot(x, x)
ax[0, 1].plot(x, x*2)
ax[1, 0].plot(x, x*x)
ax[1, 1].plot(x, x*x*x)

# set the title to subplots
ax[0, 0].set_title("Linear")
ax[0, 1].set_title("Double")
ax[1, 0].set_title("Square")
ax[1, 1].set_title("Cube")

# set spacing
fig.tight_layout()
plt.show()

Output:

Example 2: (Using title.set_text() method)

We can also add title to subplots in Matplotlib using title.set_text() method, in similar way to set_title() method.

Python3
# importing packages
import numpy as np
import matplotlib.pyplot as plt

# create data
x=np.array([1, 2, 3, 4, 5])

# making subplots
fig, ax = plt.subplots(2, 2)

# set data with subplots and plot
ax[0, 0].plot(x, x)
ax[0, 1].plot(x, x*2)
ax[1, 0].plot(x, x*x)
ax[1, 1].plot(x, x*x*x)

# set the title to subplots
ax[0, 0].title.set_text("Linear")
ax[0, 1].title.set_text("Double")
ax[1, 0].title.set_text("Square")
ax[1, 1].title.set_text("Cube")

# set spacing
fig.tight_layout()
plt.show()

Output:

Example 3: (Using plt.gca().set_title() method)

If you use Matlab-like style in the interactive plotting, then you could use plt.gca() to get the reference of the current axes of the subplot and combine set_title() method to set title to the subplots in Matplotlib.

Python3
# importing packages
import numpy as np
import matplotlib.pyplot as plt

# create data
x=np.array([1, 2, 3, 4, 5])

# making subplots
fig, ax = plt.subplots(2, 2)

# set data with subplots and plot
title = ["Linear", "Double", "Square", "Cube"]
y = [x, x*2, x*x, x*x*x]

for i in range(4):
      
    # subplots
    plt.subplot(2, 2, i+1) 
    
    # plotting (x,y)
    plt.plot(x, y[i]) 
    
    # set the title to subplots
    plt.gca().set_title(title[i]) 

# set spacing
fig.tight_layout()
plt.show()

Output :

Example 4: (Using plt.gca().title.set_text() method)

If you use Matlab-like style in the interactive plotting, then you could use plt.gca() to get the reference of the current axes of the subplot and combine title.set_text() method to set title to the subplots in Matplotlib.

Python3
# importing packages
import numpy as np
import matplotlib.pyplot as plt

# create data
x=np.array([1, 2, 3, 4, 5])

# making subplots
fig, ax = plt.subplots(2, 2)

# set data with subplots and plot
title = ["Linear","Double","Square","Cube"]
y = [x, x*2, x*x, x*x*x]

for i in range(4):
      
    # subplots
    plt.subplot(2, 2, i+1)
    
    # plotting (x,y)
    plt.plot(x, y[i])
    
    # set the title to subplots
    plt.gca().title.set_text(title[i]) 

# set spacing
fig.tight_layout()
plt.show()

Output :



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4