怎么使用Python绘制分段函数
时间:2023-04-18 23:02
具体如下: 如上图所示的分段函数如何在Python中绘制出来? 我们换个例子: 结果展示为: 以上就是怎么使用Python绘制分段函数的详细内容,更多请关注Gxl网其它相关文章!import matplotlib.pyplot as pltimport numpy as npdef f(x): if x <= -1: return -0.5 - x if -1 < x <= 1: return 0.5 * (x ** 2) else: return x - 0.5x = np.linspace(-3, 3)y = []for i in x: y_1 = f(i) y.append(y_1)plt.plot(x, y)plt.grid()plt.show()
import matplotlib.pyplot as pltimport numpy as npdef f(x): if x <= -1: return 1 if -1 < x <= 1: return 0.5 * (x ** 2) else: return 1x = np.linspace(-3, 3)y = []for i in x: y_1 = f(i) y.append(y_1)y_2 = x ** 2plt.plot(x, y)plt.grid()plt.show()