add title to a graph subplot in python

To add a title to a subplot in Matplotlib, you can use the set_title() method. Here's an example:

main.py
import matplotlib.pyplot as plt

# create a figure with two subplots
fig, (ax1, ax2) = plt.subplots(1, 2)

# plot some data on the first subplot
ax1.plot([1, 2, 3], [4, 5, 6])

# add a title to the first subplot
ax1.set_title('First Subplot')

# plot some data on the second subplot
ax2.plot([3, 2, 1], [6, 5, 4])

# add a title to the second subplot
ax2.set_title('Second Subplot')

plt.show()
395 chars
19 lines

In this example, ax1 and ax2 are subplots within the figure. We use the set_title() method to add a title to each subplot. The first argument to this method is the title string.

related categories

gistlibby LogSnag