close

How to put text outside python plots?

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How to put text outside python plots in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

How to put text outside python plots?

  1. How to put text outside python plots?

    It's probably best to define the position in figure coordinates instead of data coordinates as you'd probably not want the text to change its position when changing the data.

  2. put text outside python plots

    It's probably best to define the position in figure coordinates instead of data coordinates as you'd probably not want the text to change its position when changing the data.

Method 1

It’s probably best to define the position in figure coordinates instead of data coordinates as you’d probably not want the text to change its position when changing the data.

Using figure coordinates can be done either by specifying the figure transform (fig.transFigure)

plt.text(0.02, 0.5, textstr, fontsize=14, transform=plt.gcf().transFigure)

or by using the text method of the figure instead of that of the axes.

plt.gcf().text(0.02, 0.5, textstr, fontsize=14)

In both cases the coordinates to place the text are in figure coordinates, where (0,0) is the bottom left and (1,1) is the top right of the figure.

At the end you still may want to provide some extra space for the text to fit next to the axes, using plt.subplots_adjust(left=0.3) or so.

Method 2

Looks like the text is there but it lies outside of the figure boundary. Use subplots_adjust() to make room for the text:

import matplotlib.pyplot as plt

textstr = 'NSE=%.2f\nRMSE=%.2f\n'%(1, 2)
plt.xlim(2002, 2008)
plt.ylim(0, 4500)
# print textstr
plt.text(2000, 2000, textstr, fontsize=14)
plt.grid(True)
plt.subplots_adjust(left=0.25)
plt.show()
enter image description here

Summery

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read