如何在Python plt.title中添加变量?
发布于 2021-01-29 15:27:06
我正在尝试绘制许多图,并且对于每个图,我想使用一个变量来标记它们。如何将变量添加到plt.title?
例如:
import numpy as np
import matplotlib.pyplot as plt
plt.figure(1)
plt.ylabel('y')
plt.xlabel('x')
for t in xrange(50, 61):
plt.title('f model: T=t')
for i in xrange(4, 10):
plt.plot(1.0 / i, i ** 2, 'ro')
plt.legend
plt.show()
在的参数中plt.title()
,我希望t
变量随循环而变化。
关注者
0
被浏览
1627
1 个回答
-
例如:
num = 2 print "1 + 1 = %i" % num # i represents an integer
这将输出:
1 +1 = 2
您也可以使用浮点数执行此操作,并且可以选择要打印的小数位数:
num = 2.000 print "1.000 + 1.000 = %1.3f" % num # f represents a float
给出:
1.000 + 1.000 = 2.000
在示例中使用它来更新
t
图形标题: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()