import matplotlib.pyplot as plt
import numpy as np
def f(x):
if x<1/2:
return x
else x>=1/2:
return 1-x
x=np.linspace(0,1,10)
y= f(x)
答案 0 :(得分:1)
回答1:
import matplotlib.pyplot as plt
import numpy as np
def f(x):
if x <= 0.5:
return x
else:
return 1-x
x=np.linspace(0,1,10)
f2 = np.vectorize(f)
y = f2(x)
plt.figure()
plt.plot(x, y)
plt.show()
回答2:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0,1,10)
y = x.copy()
y[y >= 0.5] = 1 - y[y >= 0.5]
plt.figure()
plt.plot(x, y)
plt.show()