To annotate the maximum value in a Pyplot, we can take the following steps −
- Set the figure size and adjust the padding between and around the subplots.
- Create a new figure or activate an existing figure.
- Make a list of x and y data points.
- Plot x and y data points using numpy.
- Find the maximum in Y array and position corresponding to that max element in the array
- Annotate that point with local max.
- To display the figure, use show() method.
Example
import numpy as np import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True fig = plt.figure() ax = fig.add_subplot(111) x = np.array([1, 3, 5, 3, 1]) y = np.array([2, 1, 3, 1, 2]) line, = ax.plot(x, y) ymax = max(y) xpos = np.where(y == ymax) xmax = x[xpos] ax.annotate('local max', xy=(xmax, ymax), xytext=(xmax, ymax + 5), arrowprops=dict(facecolor='black'),) plt.show()