label a figure in matplotlib in python

To label a figure in Matplotlib, you can use the plt.annotate() function. Here's an example:

main.py
import matplotlib.pyplot as plt 

plt.plot([1, 2, 3, 4], [1, 4, 9, 16])
plt.ylabel('Y-axis')
plt.xlabel('X-axis')
plt.annotate('maximum value', xy=(3, 15), xytext=(2.5, 10),
            arrowprops=dict(facecolor='black', shrink=0.05))
plt.show()
246 chars
9 lines

In this example, we have plotted a simple line graph using plt.plot(). To add a label to the maximum point, we use the plt.annotate() function.

xy=(3, 15) specifies the location of the point to be labeled. xytext=(2.5, 10) specifies the location of the label text. arrowprops specifies the attributes of the arrow connecting the label and point.

You can adjust the coordinates and attributes to customize the label as per your requirements.

gistlibby LogSnag