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]
,我如何使標題自動更新用matplotlib和pylab自動更新圖表的標題
你想一個互動的情節,更新標題,當你移動axvlines?你的例子不是交互式的(你需要修正一些小錯誤)。有一個交互式劇情的例子[here](http://matplotlib.org/examples/pylab_examples/cursor_demo.html)。 – Emilien