Python 3 - matplitlib text inside vertical line
I am trying to use matplotlib
from Python 3 to achieve something like the image shown below:
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.
source to share
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')
and
plt.text(0.5, 0.5, 'Some Text', ha='center', va='center',rotation='vertical', bbox={'facecolor':'white', 'pad':5})
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:
source to share