Skip to content Skip to sidebar Skip to footer

Matplotlib Plot Label Along Plot Line

One for the matplotlib community: Say I have a straight line give by: plot([37, 45], [-0.67778, -0.67778], '--k', lw=1.2) Am I able to add a label to this line along the line rath

Solution 1:

Below is an example simply to show how it can be done without consideration for appearance. For more information about annotating plots please see this detailed demonstration.

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')

Yields:

_soannotate.png


Post a Comment for "Matplotlib Plot Label Along Plot Line"