使用Python turtle图形绘制log base 2图形

使用Python turtle图形绘制log base 2图形,python,turtle-graphics,Python,Turtle Graphics,我试过这个: if wave_type == "log": emily.up () emily.goto(0,1) emily.down () for x in range(100): y = math.log( x ) / math.log( 2 ) elaine.goto(x, y) 但它不起作用。请帮助日志(0)未定义。因此,在for循环的第一次迭代中,您的代码将出错(x将为0) 也许你想要范围(1100)?或者最好在1附近有更多的x值,因为这是图形变化最

我试过这个:

if wave_type == "log":
  emily.up ()
  emily.goto(0,1)
  emily.down ()
  for x in range(100):
    y = math.log( x ) / math.log( 2 )
    elaine.goto(x, y)
但它不起作用。请帮助

日志(0)未定义。因此,在for循环的第一次迭代中,您的代码将出错(x将为0)

也许你想要范围(1100)?或者最好在1附近有更多的x值,因为这是图形变化最大的地方,以获得更平滑的绘图。

这是一个粗略的开始——因为
range()
无法处理浮点,我们使用10倍的范围,然后在使用该值时除以10。(查看numpy中的
arange()
作为解决此问题的一种方法。)我还绘制了一个较小的区域,并使用
setworldcoordinates()
强制执行该操作,因此我们可以看到图形中与X轴相交的有趣部分:

import math
from turtle import Turtle, Screen

screen = Screen()
screen.setworldcoordinates(0, -5, 10, 5)

emily = Turtle(visible=False)
emily.forward(10)
emily.penup()

for x in range(1, 100):
    y = math.log(x / 10) / math.log(2)
    emily.goto(x / 10, y)
    emily.pendown()

screen.exitonclick()
您可以填充Y轴、添加记号、标记轴、增加分辨率等,如您所见:


艾米丽和伊莲是什么?当您尝试运行该程序时,会出现什么错误?请阅读