2017-05-03 632 views
3

我想绘制大量的图表,并且对于每个图表,我想使用一个变量来标记它们。我如何添加一个变量plt.title?例如:如何添加一个变量到Python 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为与循环变量变化。

回答

2

您可以使用%更改字符串中的值。文档可以发现here.

例如:

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

谢谢!有用。 – JoeJackJessieJames

+0

@JoeJackJessieJames您的欢迎。如果他们回答了你的问题,请记住上传并接受答案 – DavidG

0

将所有绘图函数都放在for循环中,特别是对于范围内的索引(绘图数)。使用每个图的标题标题中的索引。或者,也可以制作一个子图。

0

您可以使用打印格式。

  1. plt.title('f model: T= {}'.format(t))
  2. plt.title('f model: T= %d' % (t))·C风格的打印