【发布时间】:2021-06-03 21:24:58
【问题描述】:
在这里,我允许将数字作为字符串输入,然后将其转换为列表,最后我再次将 x 和 y 设置为空列表,但它使用每次迭代的数据值输出图形这样图形输出就有 3 条不同的线,而不是只有一条。
import matplotlib as mpl
import matplotlib.pyplot as plt
import os
xyz = int(input("How many graphs would you like to create? "))
n=1
for i in range(xyz):
x = input("Enter a your x variables, starting from the smallest with a space between each: ")
x=x.split() #takes the input and turns it into a list
print(x)
y = input("Enter a your y variables, starting from the smallest with a space between each: ")
y=y.split()
plt.plot(x,y)
plt.savefig("Desktop/python/graph"+str(n)+".png")
x = []
y = []
n=n+1
【问题讨论】:
-
您在每次迭代中将
x和y设置为新值。在您显示的代码中,在迭代结束时将它们设置为空列表是多余的。
标签: python python-3.x matplotlib