【问题标题】:Drawing dot diagram by reading Excel coordinates using Turtle使用 Turtle 通过读取 Excel 坐标绘制点图
【发布时间】:2017-02-07 23:53:22
【问题描述】:

这是我第一次发帖。我对 Python 真的很陌生,几周前刚开始上课。我正在尝试读取一个包含 (x,y) 坐标的 Excel 文件,在读取它之后,我将不得不相应地绘制点。

我为我的点创建了一个函数,读取文件并将它们分成两个列表 x[] 和 y[]。我无法将 x[] 和 y[] 中的 X、Y 值传递给我的 dot() 函数。

我在网上搜索过类似的问题,但没有运气,我确信这是因为我对编程缺乏经验。希望能得到大家的一些建议。

我已经在下面发布了我的代码。

非常感谢。

import turtle


def dot(x, y):
t = turtle.Turtle()
t.pensize(2)
t.up()
t.goto(x, y)
t.color("red")
t.down()
t.begin_fill()
t.circle(25)
t.color("red")
t.end_fill()
turtle.done()


def a():
x, y = [], []
handle = open("SineWave.csv")
for line in handle:
    line = line.rstrip()
    line = line.split(",")
    x.append(line[0])
    y.append(line[1])
    x = [int(n) for n in x]
    y = [int(n) for n in y]
i = 0
while i < len(x):
    print (x[i])
    i += 1


def b():
x, y = [], []
handle = open("SineWave.csv")
for line in handle:
    line = line.rstrip()
    line = line.split(",")
    x.append(line[0])
    y.append(line[1])
    x = [int(n) for n in x]
    y = [int(n) for n in y]
i = 0
while i < len(y):
    print(y[i])
    i += 1

dot(a(),b())

【问题讨论】:

    标签: python excel turtle-graphics


    【解决方案1】:

    您的代码似乎有点乱,有些块重复,有些块丢失。以下是我对您正在尝试做的事情的近似。不幸的是,您没有提供任何示例数据,所以我实际上无法完成代码并显示它会输出什么:

    from turtle import Turtle, Screen
    
    def read_points():
        handle = open("SineWave.csv")
    
        points = []
    
        for line in handle:
            my_x, my_y = line.rstrip().split(",")
            points.append((float(my_x), float(my_y)))
    
        return points
    
    points = read_points()
    
    yertle = Turtle(visible=False)
    yertle.up()
    yertle.color("red")
    
    for point in points:
        yertle.goto(point)
        yertle.dot(10)
    
    screen = Screen()
    screen.exitonclick()
    

    一旦您将数据拟合到上述内容并使其运行,您可能需要阅读有关海龟方法setworldcoordinates() 的信息,该方法将允许您调整窗口的坐标系以更好地拟合您使用的数据正在工作。

    【讨论】:

    • 非常感谢,这个我慢慢看。
    猜你喜欢
    • 2021-05-17
    • 2014-11-20
    • 1970-01-01
    • 2016-09-22
    • 2013-02-01
    • 2013-05-15
    • 1970-01-01
    • 2017-07-14
    • 1970-01-01
    相关资源
    最近更新 更多