[英]How can I plot this piecewise function in python?
尝试使用matplotlib
的pyplot
package。它具有非常人性化的绘图功能。 这是我绘制分段 function 的方法:
import matplotlib.pyplot as plt
import numpy as np
# boundaries
xmin = -3
xmax = 3
N = 1001 # resolution of points between [xmin, xmax]
# create x axis and empty y array
x = np.linspace(xmin, xmax, N)
y = np.zeros(N)
# apply the piecewise operations:
y[x<0] = np.sin(x[x<0])
y[(0<x) & (x<1)] = np.arctan(x[(0<x) & (x<1)])
y[1<x] = np.pi*x[1<x]**3/4
# plot
plt.plot(x, y)
plt.show()
在绘图之前,我使用带有分段条件的数组切片来编辑y
数组。 希望这可以帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.