输入3次,生成3个图函数



首先感谢您的帮助。正如我在title中所说的,我想要输入3次并生成3个图形函数

%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
times = range(1, 1)
for i in times:

def my_func2(x):
a=int(input())
return x**a
x=np.linspace(0,2)
y=my_func2(x)
plt.plot(x, y)
plt.xlabel("x",size=14)
plt.ylabel("y",size=14)
plt.grid()
plt.show()
i=i+1

你有错误的缩进-这会改变Python中的一切。您尝试在my_func2()中绘图,并在def my_func2(x):中运行y=my_func2(x)-因此您永远不会运行此函数。

如果你想重复3次,那就用range(3)代替range(1, 1),不用i = i + 1

%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
# --- functions ---
def my_func2(x):
a = int(input())  
return x**a
# --- main ---
for i in range(3):
x = np.linspace(0,2)
y = my_func2(x)
plt.plot(x, y)
plt.xlabel("x", size=14)
plt.ylabel("y", size=14)
plt.grid()
plt.show()

但是如果没有函数我也会这样做

%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
# --- main ---
for i in range(3):
a = int(input())  
x = np.linspace(0,2)
y = x**a
plt.plot(x, y)
plt.xlabel("x", size=14)
plt.ylabel("y", size=14)
plt.grid()
plt.show()

最新更新