close

How to add a variable to Python plt.title?

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How to add a variable to Python plt.title in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

How to add a variable to Python plt.title?

  1. How to add a variable to Python plt.title?

    You can use print formatting.
    plt.title('f model: T= {}'.format(t)) or
    plt.title('f model: T= %d' % (t)) # c style print

  2. add a variable to Python plt.title

    You can use print formatting.
    plt.title('f model: T= {}'.format(t)) or
    plt.title('f model: T= %d' % (t)) # c style print

Method 1

You can change a value in a string by using %. Documentation can be found here.

For example:

num = 2
print "1 + 1 = %i" % num # i represents an integer

This will output:

1 + 1 = 2

You can also do this with floats and you can choose how many decimal place it will print:

num = 2.000
print "1.000 + 1.000 = %1.3f" % num # f represents a float

gives:

1.000 + 1.000 = 2.000

Using this in your example to update t in the figure title:

plt.figure(1)
plt.ylabel('y')
plt.xlabel('x')

for t in xrange(50,61):
    plt.title('f model: T=%i' %t)

    for i in xrange(4,10):
        plt.plot(1.0/i,i**2,'ro')

    plt.legend
    plt.show()

Method 2

You can use print formatting.

  1. plt.title('f model: T= {}'.format(t)) or
  2. plt.title('f model: T= %d' % (t)) # c style print

Summery

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read