python用turtle画正弦函数
时间: 2023-04-22 18:02:55 浏览: 148
使用Turtle画正螺旋线的方法
可以使用Python中的turtle模块来画正弦函数。具体步骤如下:
1. 导入turtle模块
```python
import turtle
```
2. 创建一个画布和画笔
```python
canvas = turtle.Screen()
pen = turtle.Turtle()
```
3. 设置画笔的颜色和粗细
```python
pen.color("blue")
pen.pensize(2)
```
4. 设置起始点和终止点
```python
start_x = -200
end_x = 200
```
5. 循环画出正弦函数的图像
```python
for x in range(start_x, end_x):
y = math.sin(x / 50) * 100
pen.goto(x, y)
```
6. 关闭画布
```python
canvas.exitonclick()
```
完整代码如下:
```python
import turtle
import math
canvas = turtle.Screen()
pen = turtle.Turtle()
pen.color("blue")
pen.pensize(2)
start_x = -200
end_x = 200
for x in range(start_x, end_x):
y = math.sin(x / 50) * 100
pen.goto(x, y)
canvas.exitonclick()
```
阅读全文