VOOZH about

URL: https://www.geeksforgeeks.org/python/add-text-inside-the-plot-in-matplotlib/

⇱ Add Text Inside the Plot in Matplotlib - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Add Text Inside the Plot in Matplotlib

Last Updated : 3 Oct, 2025

The matplotlib.pyplot.text() function is used to add text inside the plot. The syntax adds text at an arbitrary location of the axes. It also supports mathematical expressions.

Examples of Adding Text Inside the Plot

1. Adding Mathematical Equations

In this example, we plot a parabola (y = xΒ²) and add text inside the plot.

Output:

πŸ‘ Image

Explanation: np.arange(-10, 10, 0.01) creates x-values, y = x**2 forms the parabola, text Parabola Y=x2 is added at (-5, 60) and the green curve is plotted with axis labels.

2. Adding a Rectangular Box Around Text

We can also place text inside a colored box using the bbox parameter.

Output:

πŸ‘ Image

Explanation: The parabola y=x2 is plotted in green and text Parabola Y=x2is added at (-5, 60) inside a semi-transparent red box using bbox.

3. Adding Simple Labels ("Sine wave")

We can add descriptive labels like β€œSine wave” at any point in the plot.

Output:

πŸ‘ Image

Explanation: np.arange(0, 10, 0.1) generates x-values, y = np.sin(x) creates the sine wave, text Sine wave is added at (3.5, 0.9) and the curve is displayed with axis labels.

4. Adding Text with Annotation (Arrows)

Annotations are useful when you want to point to a specific part of a plot.

Output:

πŸ‘ Image

Explanation: A bar chart of student marks is plotted, text Student Marks is added at (3, 7) and plt.annotate() highlights the highest score (Raju) with a green label and red arrow.

Comment