1. 展示数组切片
a = [i for i in range(10)]
print(a[:3])
print(a[:-3:-1])
print(a[::2])
print(a[::-1])
第2关:绘制公式
import numpy as np
import matplotlib.pyplot as plt
class Solution:
def solve(self, v0, g):
"""
:type v0, g: int, int
:rtype: None
"""
t=np.linspace(0,2*v0/g,50)
y=v0*t-g*t*t/2
plt.plot(t,y)
plt.xlabel('time(s)')
plt.ylabel('height(m)')
plt.show()
plt.savefig("step2/stu_img/student.png")
第3关:绘制多参数公式
import numpy as np
import matplotlib.pyplot as plt
class Solution:
def solve(self, v0):
"""
:type v0: List[int]
:rtype: None
"""
g=9.81
for v in v0:
t=np.linspace(0,2.0*v/g,50)
y=v*t-g*t*t/2
plt.plot(t,y)
plt.xlabel('time(s)')
plt.ylabel('height(m)')
plt.show()
plt.savefig("step3/stu_img/student.png")
第4关:指定图中轴的范围
class Solution:
def solve(self, v0):
"""
:type v0: List[int]
:rtype: None
"""
import numpy as np
import matplotlib.pyplot as plt
g=9.81
t1=0
y1=0
for v in v0:
t=np.linspace(0,2.0*v/g,50)
if t1<max(t):
t1=max(t)
y=v*t-g*t*t/2
if y1<max(y):
y1=max(y)
plt.plot(t,y)
plt.xlabel('time(s)')
plt.ylabel('height(m)')
plt.axis([0,t1,0,y1*1.1])
plt.savefig("step4/stu_img/student.png")
第5关:绘制精确和不精确的华氏-摄氏转换公式
class Solution:
def solve(self, s, e):
"""
:type s, e: int, int
:rtype: None
"""
import numpy as np
import matplotlib.pyplot as plt
F=np.linspace(s,e,50)
C1=(F-32)*5/9.0
C2=(F-30)/2.0
plt.plot(F,C1,'b-')
plt.plot(F,C2,'r.')
plt.show()
plt.savefig("step5/stu_img/student.png")
6. 绘制球的轨迹
class Solution:
def solve(self, y0, theta, v0):
"""
:type y0, theta, v0: int, int, int
:rtype: None
"""
import numpy as np
from matplotlib import pyplot as plt
g = 9.81
theta = theta/180.0*np.pi
a = -1/(2*v0**2)*g/(np.cos(theta)**2)
b = np.tan(theta)
c = y0
delta = np.sqrt(b**2-4*a*c)
x0 = (-b-delta)/(2*a)
x1 = (-b+delta)/(2*a)
xmin = min(x0, x1)
xmax = max(x0, x1)
x = np.linspace(0,xmax,51)
y = x*np.tan(theta)-1/(2*v0**2)*g*(x**2)/(np.cos(theta)**2)+y0
plt.plot(x,y)
plt.axis([min(x),max(x),0,max(y)*1.1])
plt.show()
plt.savefig("step6/stu_img/student.png")
7. 绘制文件中的双列数据
class Solution:
def solve(self, file):
"""
:type file: str
:rtype: None
"""
import numpy as np
import matplotlib.pyplot as plt
a=np.loadtxt(file, dtype=np.float)
x=np.array(a[:,0])
y=np.array(a[:,1])
print(np.mean(y),np.max(y),np.min(y))
plt.plot(x,y)
plt.savefig("step7/stu_img/student.png")