4

The following code represents two different functions, sigmoid(x), and logit(x). How is it possible to insert the dynamic labels, a and b into the plt.text() which derived from matplotlib.pyplot?

import math
import matplotlib.pyplot as plt

plt.ylabel("F(x)")
plt.xlabel("x")

a = 6
b = 0.9985

def sigmoid(x):
    return 1/(1+math.exp(-x))

#LOU jit
def logit (x):
    return math.log(x/(1-x))



z = sigmoid(a)
l = logit(b)

print(z)
print(l)

font = {
        'family': 'serif',
        'color' : 'green',
        'weight': 'normal',
        'size'  :  9
}
plt.plot([a,z],[b,l],'ro')
plt.text(a,z,'Sigmoid(a)',fontdict=font)
plt.text(b,l,'Logit(b)',fontdict=font)
plt.axis([0,10,0,50])

plt.grid(True)

plt.show()

1 Answer 1

7

Using % operator like the following line:

plt.text(a,z,'Sigmoid(%s)'%(a),fontdict=font)
Sign up to request clarification or add additional context in comments.

1 Comment

Well done on finding the solution yourself! Just FYI, python has a new .format() paradigm which is more powerful than the old % one. See docs.python.org/3/library/string.html#string-formatting and pyformat.info

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.