简体   繁体   English

如何将点从函数转换为列表以绘制完整图形?

[英]How do I turn points from function in to a list to plot a complete graph?

So I have made a function plot_line(p1,p2) that takes two points as input arguments and plots the line between them.所以我制作了一个函数 plot_line(p1,p2) ,它将两个点作为输入参数并绘制它们之间的线。 The two input arguments are lists or tuples specifying x- and y-coordinates, ie, p1 =(x1,y1).两个输入参数是指定 x 和 y 坐标的列表或元组,即 p1 =(x1,y1)。 Now further i want to make a function for example: complete_graph(points) that takes a list of points and plots the complete graph on those points.现在我想进一步创建一个函数,例如: complete_graph(points) 获取点列表并在这些点上绘制完整的图形。 I have tried to modify the plot_line function from so that it only calls plot() but not show().我试图修改 plot_line 函数,以便它只调用 plot() 而不是 show()。 I wish for the complete graph to be drawn by looping over the points and calling plot_line for each pair, and finally calling show()after the loop, how would I do that?我希望通过循环点并为每一对调用 plot_line 来绘制完整的图形,最后在循环后调用 show() ,我该怎么做?

I have this code for now:我现在有这个代码:

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()

The plot is supposed to look like this in the end result, but I don't get it like that:情节在最终结果中应该是这样的,但我不明白:

想要的情节

This will make you a nice little house whit a star inside (and should work for any list of tuples)这将使您成为一个漂亮的小房子,里面有一颗星星(并且应该适用于任何元组列表)

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()

If you need to get the input from the user you can keep your input code.如果您需要从用户那里获取输入,您可以保留您的输入代码。

Note that I am iterating only from data[i+1:] in the inner loop and incrementing i in the outer loop through enumerate to avoid duplicate lines (every point should be connected only once with the others)请注意,我仅在内循环中从data[i+1:]进行迭代,并通过enumerate在外循环中递增 i 以避免重复行(每个点应仅与其他点连接一次)

Output:输出:

在此处输入图片说明

I think i made something that works as what you want:我想我做了一些你想要的东西:

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM