Python 3 - matplitlib text inside vertical line

I am trying to use matplotlib

from Python 3 to achieve something like the image shown below:

enter image description here

A similar question was asked here , but the accepted answer is not enough for my need. I need to add some text in the middle of the dotted line (which I can plot with a function plt.axvline()

).

Here is what I have tried

import matplotlib.pylab as plt
plt.hist(some_data)
plt.axvline(0.5, color='k', linestyle='--')
plt.text(0.5, 0.5, 'Some Text', ha='center', va='center',rotation='vertical')
plt.show()

      

If I can put this text in the middle of the dotted line, that would be great.

+3


source to share


1 answer


This is not a solution but more of a workaround. You can try setting a background color for the text, or adding a bounding rectangle with a specific color to it that will obscure the line. This will make the text appear on the line.

You would accomplish it like this:

plt.text(0.5, 0.5, 'Some Text', ha='center', va='center',rotation='vertical', backgroundcolor='white')

      

enter image description here

and



plt.text(0.5, 0.5, 'Some Text', ha='center', va='center',rotation='vertical', bbox={'facecolor':'white', 'pad':5})

      

enter image description here

Of course, there is a problem when this line is superimposed on the histogram of a different color and then you will need to match the background or window color to the histogram. It will look something like this:

enter image description here

+4


source







All Articles