JoeyBright
JoeyBright

Reputation: 351

How to insert a variable into a text method of matplotlib

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

Upvotes: 4

Views: 17592

Answers (1)

JoeyBright
JoeyBright

Reputation: 351

Using % operator like the following line:

plt.text(a,z,'Sigmoid(%s)'%(a),fontdict=font)

Upvotes: 7

Related Questions