I am trying to use matplotlib
from Python 3 to achieve something like the image shown below:
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.
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')
and
plt.text(0.5, 0.5, 'Some Text', ha='center', va='center',rotation='vertical', bbox={'facecolor':'white', 'pad':5})
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With