1

I would like to be able to display an object; call it "Slope", on my matplotlib graph. Eg:

import numpy as np
import matplotlib.pyplot as plt

range1 = a[(-5. <= a) & (-3. >= a)]
range2 = b[(-5. <= a) & (-3. >= a)]

'''Calculate slope value from endpoints in the data range (linear).'''

xslopeentry1 = range1[0]
xslopeentry2 = range1[-1]
yslopeentry1 = range2[0]
yslopeentry2 = range2[-1]
Slope = (yslopeentry2-yslopeentry1)/(xslopeentry2-xslopeentry1)

plt.plot(range1,range2)
plt.show()

Now, how will I be able to 'print', or display the value obtained for 'Slope' on my plot?

1 Answer 1

3

There are several options for adding text in matplotlib. The best explanation of them comes from the documentation.

For your purpose, there are 3 options that may make sense:

1.) Text relative to axes:

matplotlib.pyplot.text(Slope,x,y)

where x and y are the coordinates of the text relative to the axes.

2.) Text relative to figure:

matplotlib.pyplot.figtext(Slope,x,y)

where x and y are the coordinates of the text relative to the figure

3.) Annotation:

This creates a piece text annotation referring to a specific data point. This doesn't make as much sense here, but it does allow for easy arrow creation, if you wanted an arrow pointing to the line the slope related to.

matplotlib.pyplot.annotate(Slope, xy=(xx, yy), xytext=(x, y),
        arrowprops=dict(facecolor='black', shrink=0.05))

where x and y are the text coordinates, and xx, yy are the coordinates of the point the arrow points to.

**Note that the examples above only place the value of the slope on the plot. If you want "Slope: value" instead, replace "Slope" above with:

"Slope: {0}".format(Slope)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.