![]() |
VOOZH | about |
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.
In this example, we plot a parabola (y = xΒ²) and add text inside the plot.
Output:
π ImageExplanation: 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.
We can also place text inside a colored box using the bbox parameter.
Output:
π ImageExplanation: 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.
We can add descriptive labels like βSine waveβ at any point in the plot.
Output:
π ImageExplanation: 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.
Annotations are useful when you want to point to a specific part of a plot.
Output:
π ImageExplanation: 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.