【发布时间】: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