栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

educoder 5-6Python计算思维训练——数组和曲线绘制练习(二)

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

educoder 5-6Python计算思维训练——数组和曲线绘制练习(二)

目录
  • 1. 展示数组切片
  • 第2关:绘制公式
  • 第3关:绘制多参数公式
  • 第4关:指定图中轴的范围
  • 第5关:绘制精确和不精确的华氏-摄氏转换公式
  • 6. 绘制球的轨迹
  • 7. 绘制文件中的双列数据

1. 展示数组切片
a = [i for i in range(10)]
#输出数组a前3个元素
print(a[:3])
#输出数组a后2个元素
print(a[:-3:-1])
#输出数组a中下标为偶数的元素
print(a[::2])
#逆序输出数组a
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
        """
        #请在此按照“编程要求”填写代码
        #********** Begin *********#
        t=np.linspace(0,2*v0/g,50)#linspace在start和stop之间返回均匀间隔的数据
        y=v0*t-g*t*t/2

        plt.plot(t,y)

        plt.xlabel('time(s)')
        plt.ylabel('height(m)')
        plt.show()
        ##********** End **********#
        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
        """
        #请在此按照“编程要求”填写代码
        #********** Begin *********#
        g=9.81
        #代表t的取值
        
        #处理y值
        for v in v0:
            t=np.linspace(0,2.0*v/g,50)
            y=v*t-g*t*t/2
            plt.plot(t,y)
        #x、y标签
        plt.xlabel('time(s)')
        plt.ylabel('height(m)')
        #x,y刻度
        
    
        plt.show()
        ##********** End **********#
        plt.savefig("step3/stu_img/student.png")

第4关:指定图中轴的范围
class Solution:
    def solve(self, v0):
        """
        :type v0: List[int]
        :rtype: None
        """
        #请在此按照“编程要求”填写代码
        #********** Begin *********#
        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 
第5关:绘制精确和不精确的华氏-摄氏转换公式 
class Solution:
    def solve(self, s, e):
        """
        :type s, e: int, int
        :rtype: None
        """
        #请在此按照“编程要求”填写代码
        #********** Begin *********#
        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()
        ##********** End **********#
        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
        """
        #请在此按照“编程要求”填写代码
        #********** Begin *********#
        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()
        ##********** End **********#
        plt.savefig("step6/stu_img/student.png")



7. 绘制文件中的双列数据
class Solution:
    def solve(self, file):
        """
        :type file: str
        :rtype: None
        """
        #请在此按照“编程要求”填写代码
        #********** Begin *********#
        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)

        ##********** End **********#
        plt.savefig("step7/stu_img/student.png")


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/348363.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号