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

这篇博客通过一系列实例展示了如何使用Python进行图形绘制,包括数组切片操作、绘制数学公式、多参数公式、指定图表轴范围、华氏-摄氏转换公式以及从文件中读取双列数据并绘制。每个实例都详细解释了代码实现,旨在帮助读者掌握科学计算和数据可视化的基础技巧。
摘要由CSDN通过智能技术生成

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<max(t):
                t1=max(t)#t为数组

            y=v*t-g*t*t/2
            if y1<max(y):#y为数组
                y1=max(y)
            
            plt.plot(t,y)
            
        #x、y标签
        plt.xlabel('time(s)')
        plt.ylabel('height(m)')

        plt.axis([0,t1,0,y1*1.1])#注意[]
        
        ##********** End **********#
        plt.savefig("step4/stu_img/student.png")


第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")


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值