使用matplotlib和pylab自动更新图表的标题

时间:2016-08-16 08:30:03

标签: python matplotlib

import numpy as np
import matplotlib.pyplot as plt 
%pylab inline

def fun (x): #piecewise functions
    if x< -1:
        return 2*x + 4 
    elif -1<= x <= 1:
        return 2*x**2
    elif x>1:
        return 2 

vfun = np.vectorize(fun)
a=-4         #as provided in question paper
b=5
N=50
x = np.linspace(a, b, N)
pylab.xlim(a, b)
pylab.ylim(vfun(x).min(), vfun(x).max())
axvline(x=-1.,color='k',ls='dashed')
axvline(x=1.,color='k',ls='dashed')
y= vfun(x)

pylab.xlabel('x')    #labeling
pylab.ylabel('y')
pylab.title('My First Plot')


plt.plot(x, y, '.')  # dotted style of line
plt.show()

如果对间隔进行了更改,如何更新标题。例如,如果我的标题是"f(x) E [-4,5], N=50"。如果间隔更改为[-2,3],如何使标题自动更新

2 个答案:

答案 0 :(得分:1)

您可以使用str.format()插入a,b和N的当前值:

pylab.title('f(x) E [{0},{1}], N={2}'.format(a,b,N))

答案 1 :(得分:-1)

这个怎么样?

pylab.title("f(x) E ["+str(a)+","+str(b)+"], N="+str(N))

而不是

pylab.title('My First Plot')
相关问题