【问题标题】:How do I turn points from function in to a list to plot a complete graph?如何将函数中的点转换为列表以绘制完整的图表?
【发布时间】:2020-10-04 20:48:29
【问题描述】:

所以我创建了一个函数 plot_line(p1,p2),它将两个点作为输入参数并绘制它们之间的线。两个输入参数是 指定 x 和 y 坐标的列表或元组,即 p1 =(x1,y1)。现在我想进一步制作一个函数,例如:complete_graph(points),它需要一个列表 点并在这些点上绘制完整的图形。我试图修改 plot_line 函数,以便它只调用 plot() 而不是 show()。我希望通过循环点并为每一对调用 plot_line 来绘制完整的图形,最后在循环之后调用 show(),我该怎么做?

我现在有这个代码:

import matplotlib.pyplot as plt
from math import sqrt

"Task a)"
#call function tuple
def cor_tup(x, y): #coordinates for tuple
    return(x,y)

def plotline(p1,p2):
    x=(p1[0],p2[0]) #x values
    y=(p1[1],p2[1])#y values
    plt.plot(x, y, "-o", label="x,y") #plot the points in graph
    
x1=int(input('enter first desired x value:'))
y1=int(input('enter first desired y value:'))
x2=int(input('enter second desired x value:'))
y2=int(input('enter second desired y value:'))

p1=cor_tup(x1,y1)
p2=cor_tup(x2,y2)

plotline(p1,p2)
plt.xlabel('x')
plt.ylabel('y')
plt.legend
plt.axis([-5,5,-5,5])
plt.title('Linear function')
plt.show()

情节在最终结果中应该是这样的,但我不明白:

【问题讨论】:

    标签: python matplotlib plot graph graph-theory


    【解决方案1】:

    这将使您成为一个漂亮的小房子,里面有一颗星星(并且应该适用于任何元组列表)

    import matplotlib.pyplot as plt
    
    def plotline(p1, p2):
        x=(p1[0],p2[0]) #x values
        y=(p1[1],p2[1])#y values
        plt.plot(x, y, "-b", label="x,y") #plot the points in graph
    
    data = [(0,0), (0,1), (1,0), (1,1), (0.5, 1.5)]
    
    for i, xy in enumerate(data):
        for xy2 in data[i+1:]:
            plotline(xy, xy2)
    
    x = [d[0] for d in data]
    y = [d[1] for d in data]
    plt.plot(x, y, 'o',color='r')
    plt.show()
    

    如果您需要从用户那里获取输入,您可以保留您的输入代码。

    请注意,我只在内循环中从 data[i+1:] 迭代,并在外循环中通过 enumerate 递增 i 以避免重复行(每个点只能与其他点连接一次)

    输出:

    【讨论】:

    • 我很喜欢这是多么少的代码,但我不太了解带有枚举的 for 循环,它如何加入顶点而不重绘它们之间的现有线?
    • 您从第一个点 (i=0) 开始,并使用内循环将所有连接线绘制到其他点,但从 i+1 开始,因为您不想连接该点与自己。然后您移动到第二个点 (i=1),但您已经在前一次迭代中绘制了连接点 i=0 和 i=1 的线(当 i=0 时)。所以你需要跳过前一点和当前点。通过在内部循环中使用data[i+1],您可以确保跳过将点与其自身以及与之前已连接的点连接。这说明清楚了吗?
    • 啊,是的,现在更有意义了。我仍然对 for i, xy in enumerate(data) 的语法感到困惑,但我并不经常在 for 循环中使用 enumerate 或 for i, n 两个变量。
    • enumerate 这是一个非常有用的内置函数。从技术上讲,它返回一个元组迭代器,该迭代器由您正在枚举的可迭代对象的每个元素的索引和每个元素本身组成。简而言之,这是编写以下内容的更好方法:for i in range(len(mylist)): do_smth_with(mylist[i])
    【解决方案2】:

    我想我做了一些你想要的东西:

    import matplotlib.pyplot as plt
    from math import sqrt
    
    "Task a)"
    #call function tuple
    def cor_tup(x, y): #coordinates for tuple
        return(x,y)
    
    def plotline(p1, p2):
        x=(p1[0],p2[0]) #x values
        y=(p1[1],p2[1])#y values
        plt.plot(x, y, "-o", label="x,y") #plot the points in graph
    
    
    def Line_of_points(totalLines):
    
        listOfPoints = []
        
        for i in range(1, totalLines+1):
            currentx = int(input("Enter first x point " + str(i) + " : "))
            currenty = int(input("Enter first y point " + str(i) + " : "))
            currentPoint1 = (currentx, currenty)
    
            currentx = int(input("Enter second x point " + str(i) + " : "))
            currenty = int(input("Enter second y point " + str(i) + " : "))
            currentPoint2 = (currentx, currenty)
    
            points = (currentPoint1, currentPoint2)
            
            listOfPoints.append(points)   
        
        return listOfPoints
    
    
    def Draw_All_Points(pointList):
        counter = 0
    
        while counter < len(pointList):
            if counter <= len(pointList)-1: #so at last point, doesnt access out of list len
                firstPoint = pointList[counter][0]
                secondPoint = pointList[counter][1]
                print(firstPoint, secondPoint)
                
    
                plotline(firstPoint, secondPoint)
                
            else:
                break
    
            counter += 1
    
        
    lineOfPoints = Line_of_points(2)
    Draw_All_Points(lineOfPoints)
    
    
    
    plt.xlabel('x')
    plt.ylabel('y')
    plt.legend
    plt.axis([-5,5,-5,5])
    plt.title('Linear function')
    plt.show()
    
    

    【讨论】:

    • 如果要绘制矩形,只需将 4 传入 Line_of_points 函数,然后输入类似:0,0 0,1 2,1 2,0 0,0
    • 需要 ((0, 0),(1, 0),(0, 1),(1, 1)) 然后是点 (1, 0),(α, α) ,(0, 1),(-α, α),(-1, 0),(-α, -α),(0, -1),(α, -α) 其中α=sqrt2/2跨度>
    • 您是要连续绘制点,还是给它一个起点,然后将其绘制到终点?
    • 我想给点一个起点,然后画到一个终点,然后重新启动你知道所以有一堆线相互连接并形成一个图形。喜欢右边的i.stack.imgur.com/kQ8ZZ.png
    • 这就是你的意思(编辑过的答案),对于一个矩形,你想要一个点在 (0,0) (0,1) 然后 (0,1) (1,1) 然后 ( 1,1) (1,0) 和 (1,0)(0,0) 以及对角线类似于 (0,0)(1,1)?
    猜你喜欢
    • 1970-01-01
    • 2019-08-07
    • 1970-01-01
    • 2016-12-16
    • 2018-10-02
    • 1970-01-01
    • 1970-01-01
    • 2019-10-06
    相关资源
    最近更新 更多