Matplotlib plot Label along plot line

One for the matplotlib community:

Let's say I have a straight line:

plot([37, 45], [-0.67778, -0.67778], '--k', lw=1.2)

      

Is it possible to add a label to this line along the line and not to the legend? that is, something similar to the following (but not a contour plot, just a regular line plot):

enter image description here

+2


source to share


1 answer


Below is an example to show how this can be done without considering appearance. For more information on annotations see this detailed demo .

import matplotlib.pyplot as plt
x = [37, 45]; y = [-0.67778, -0.67778]

# as an example for where to place the text we can use the mean
xmean = sum(i for i in x) / float(len(x))
ymean = sum(i for i in y) / float(len(y))

plt.plot(x, y, '--k', lw=1.2)
plt.annotate('some text', xy=(xmean,ymean), xycoords='data')
plt.show() # or plt.savefig('filename.png')

      



Productivity:

_soannotate.png

+3


source







All Articles