How to insert a variable into a text method of matplotlib

11,302

Using % operator like the following line:

plt.text(a,z,'Sigmoid(%s)'%(a),fontdict=font)
Share:
11,302

Related videos on Youtube

JoeyBright
Author by

JoeyBright

Hi, my full name is Javad PourMostafa (pronounced /’dʒævɒd puːr’moʊs’tae’fɑː/)! I go by joeybright or joyebright wherever I can get the username. I’m currently a postgraduate member at Guilan NLP Group and a part-time lecturer at the Computer Engineering Department of the University of Guilan. I graduated with an MS degree in Software Engineering from the same university in 2019. During the master thesis, I worked on designing sentiment analysis models using deep learning architectures for low-resources corpora (case-study: Persian). My research mainly centralizes on natural language processing and deep learning. I’m also interested in AI more broadly; as to non-academic affairs, I spend my time on Unix-like server maintenance.

Updated on June 04, 2022

Comments

  • JoeyBright
    JoeyBright almost 2 years

    The following code represents two different functions, sigmoid(x), and logit(x). How it is 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()
    
  • Diziet Asahi
    Diziet Asahi over 6 years
    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
  • JoeyBright
    JoeyBright over 6 years
    Tnq, I saw its relevant documentation!