问题描述
我写了这段代码
import matplotlib.pyplot as plt
import numpy as np
fig=plt.figure()
r=np.arange(1,2)
theta=np.linspace(0,np.pi/2)
x=r*np.cos(theta)
y=r*np.sin(theta)
plt.plot(x,y)
plt.show()
但我想得到下面的图表。
我对如何设置合适的 r
范围感到困惑。
我想设置 r
的范围 1<=r<=2
,但不知道该怎么做。
如何修改我的代码?
解决方法
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
theta = np.linspace(0,np.pi/2)
for i in range(1,3):
x = []
y = []
for t in theta:
if t == 0:
y.append(0)
x.append(3-1)
elif t == np.pi/2:
y.append(3-1)
x.append(0)
else:
x.append(i*np.cos(t))
y.append(i*np.sin(t))
plt.plot(x,y)
plt.show()