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

2024-10-01 07:13:08 发布

您现在位置:Python中文网/ 问答频道 /正文

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],如何使标题自动更新


Tags: import标题间隔returnasnppltls