Joe*_*mes 14 python matplotlib python-3.x
我试图绘制大量的图表,并且对于每个图表,我想使用变量来标记它们.如何添加变量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()
Run Code Online (Sandbox Code Playgroud)
在论证中plt.title(),我希望t随着循环变化.
Dav*_*idG 20
例如:
num = 2
print "1 + 1 = %i" % num # i represents an integer
Run Code Online (Sandbox Code Playgroud)
这将输出:
1 + 1 = 2
您也可以使用浮点数执行此操作,您可以选择打印的小数位数:
num = 2.000
print "1.000 + 1.000 = %1.3f" % num # f represents a float
Run Code Online (Sandbox Code Playgroud)
得到:
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()
Run Code Online (Sandbox Code Playgroud)
您可以使用打印格式。
plt.title('f model: T= {}'.format(t)) 要么plt.title('f model: T= %d' % (t)) #c样式打印小智 6
您也可以只连接标题字符串:
x=1
y=2
plt.title('x= '+str(x)+', y = '+str(y))
Run Code Online (Sandbox Code Playgroud)
将使标题看起来像
x= 1, y = 2