Matplotlib: text center in his bbox

I need to draw some data and some vertical lines to separate interesting intervals, and then I would like to add labels to them using text. I cannot completely avoid overlapping labels with data or vertical lines, so I decided to put text bboxaround the text so that it is readable. My problem is that I cannot center it inside this frame, and this, in my opinion, is clearly visible and quite annoying.

I am doing something like this:

import numpy
import matplotlib
import matplotlib.pyplot as plt

fig=plt.figure()
plot=fig.add_subplot(111)
x=numpy.linspace(1,10,50)
y=numpy.random.random(50)
plot.plot(x,y)
plot.text(4.5,.5,'TEST TEST',\
          bbox={'facecolor':'white','alpha':1,'edgecolor':'none','pad':1})
plot.axvline(5,color='k',linestyle='solid')
plt.show()

Which creates the following graph: enter image description here

It is clear that the text is not centered in bbox. How can i change this? I spent quite a lot of time on Google, but found nothing.

EDIT:

Thanks for the suggestions so far.

, , , - . -, bbox matplotlib ( "g" ).

"g" , : enter image description here , "g" - . - - ?

+4
1

ha va:

plot.text(5.5,.5,'TEST TEST TEST TEST',
          bbox={'facecolor':'white','alpha':1,'edgecolor':'none','pad':1},
          ha='center', va='center') 

, :

plot.axvline(5.5,color='k',linestyle='solid')
plot.axhline(0.5,color='k',linestyle='solid')
+2

Source: https://habr.com/ru/post/1618572/


All Articles