
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How to automatically annotate the maximum value in a Pyplot?
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()
Output
- Related Articles
- How to annotate the points on a scatter plot with automatically placed arrows in Matplotlib?
- How to annotate a range of the X-axis in Matplotlib?
- How to annotate a heatmap with text in Matplotlib?
- How to get the list of axes for a figure in Pyplot?
- How to annotate seaborn pairplots?
- How to zoom subplots together in Matplotlib/Pyplot?
- How to Automatically Send Email Based on Cell Value in Excel?
- How to annotate each cell of a heatmap in Seaborn?
- How to select the maximum value of a column in MySQL?
- How to change the maximum value of a JSlider in Java?
- How to Annotate Matplotlib Scatter Plots?
- How to add the maximum value to HTML?
- How to fill the area under a step curve using pyplot? (Matplotlib)
- How to Annotate Bars in Grouped Barplot in Python?
- How to automatically resize a JTree in Java

Advertisements