Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Similar question has been asked here but accepted answer is not sufficient for my need. I need to add text in the middle of dotted line (which I can plot with plt.axvline() function).

Here is what I 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 dotted line, it will be great.

like image 302
n00b Avatar asked May 08 '17 07:05

n00b


1 Answers

This isn't a solution but more of a workaround really. You could try setting the background colour for the text or adding a bounding box to it with a specific colour which will eclipse the line. This will make the text appear inline.

You would implement this like so:

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 a problem arises when this line is being overlaid on a histogram of a different colour and you would then have to match the colour of background or box to the histogram. This would display something like so:

enter image description here

like image 198
YashTD Avatar answered Oct 21 '22 11:10

YashTD