educoder 5-1Python 计算思维训练——数组计算与曲线绘制(答案)

Python计算思维:数组计算与曲线绘制实战
本文通过Python探讨了数组计算和曲线绘制,包括sin函数、抛物线的绘制,使用向量化处理技术绘制函数曲线,并涉及图例与坐标设置,详细介绍了多条曲线的绘制方法。

1. 绘图函数 - 绘制 sin 函数

# 请绘制sin函数曲线

import matplotlib
matplotlib.use("Agg") # 设置平台绘图环境,勿删

import matplotlib.pyplot as plt
# 请在此添加代码实现函数细节   #
# ********** Begin *********#
x = [0, 30, 60, 90, 120, 150, 180, 210, 240, 270, 300, 330, 360]
y= [0, 0.5, 0.866, 1, 0.866, 0.5, 0, -0.5, -0.866, -1, -0.866, -0.5, 0]
plt.plot(x,y,'.')




# ********** End **********#

plt.savefig('picture/step0/fig0.png') #存储输出图像,勿删

在这里插入图片描述

2. 绘图与保存 - 抛物线函数曲线

# 请绘制抛物线曲线
import matplotlib
matplotlib.use("Agg")

x=range(0,51,1)
def f(x):
    # 请在此添加代码实现函数细节   #
    # ********** Begin1 *********#
    y=[]
    for e in x:
        y.append(3*e*e+2*e+1)
    return y
    # ********** End1 **********#

#   请在此添加代码绘制曲线并存储图像#
# ********** Begin2 *********#
import matplotlib.pyplot as plt
plt.plot(x,f(x),'r--')
plt.savefig("picture/step1/fig1.png")

# ********** End2 **********#
#注意:1、x在函数外定义;2、append要用(),不是[];3、红色是r--

在这里插入图片描述

3. 数组计算与向量化处理 - 函数曲线绘制与坐标处理

# 请绘制函数曲线
import numpy as np
from numpy import *
import matplotlib
matplotlib.use("Agg")


#   请在此添加实现代码   #
# ********** Begin *********#
def f(t):
    result=t*t*e**(-t*t)
    return result

t=np.linspace(0.0,3.0,num=50)
y=f(t)
import matplotlib.pyplot as plt
plt.plot(t,y)
plt.savefig("picture/step2/fig2.png")



# ********** End **********#

在这里插入图片描述

4. 图例与坐标设置 - 绘制多条曲线

#请在同一坐标系中绘制两条曲线


import matplotlib
matplotlib.use("Agg")

#   请在此添加实现代码   #
# ********** Begin *********#
import numpy as np
from numpy import *
t=np.linspace(0.0,3.0,num=50)
y1=[(i**2)*np.exp(-i*i) for i in t]
y2=[(i**4)*np.exp(-i*i) for i in t]

import matplotlib.pyplot as plt
plt.xlabel('t')
plt.ylabel('y')
plt.plot(t,y1,'r--')
plt.plot(t,y2,'b-o')#蓝色圆点实线:b-0o
plt.title('Plotting two curves in the same plot')
plt.legend(['y1','y2'])

plt.savefig('picture/step3/fig3.png')



# ********** End **********#

在这里插入图片描述

5.向量化处理 - 绘制函数图形

# 请编写代码实现向量化帽函数并绘制函数曲线

import matplotlib
matplotlib.use("Agg")

#   请在此添加实现代码   #
# ********** Begin *********#
import numpy as np
from numpy import *

import matplotlib.pyplot as plt

def N(x):
    return np.where(x<0,0,(np.where(x<1,x,(np.where(x<2,2-x,0)))))

x=np.linspace(-3,5,1000)
y=N(x)
plt.plot(x,y,'b-')
plt.title("Plotting hat func in this plot")
plt.savefig('picture/step4/fig4.png')



# ********** End **********#
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值