GCien
GCien

Reputation: 2349

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 rather than in the legend? i.e., something similar to the following (but rather than a contour plot, just an ordinary line plot):

enter image description here

Upvotes: 3

Views: 7119

Answers (1)

mechanical_meat
mechanical_meat

Reputation: 169274

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

Upvotes: 3

Related Questions