【问题标题】:How do I empty the lists after each iteration?每次迭代后如何清空列表?
【发布时间】: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

【问题讨论】:

  • 您在每次迭代中将 xy 设置为新值。在您显示的代码中,在迭代结束时将它们设置为空列表是多余的。

标签: python python-3.x matplotlib


【解决方案1】:

您正在绘制同一个图形。用plt.clf()清除图形。

【讨论】:

    猜你喜欢
    • 2020-01-04
    • 2014-10-31
    • 2014-01-30
    • 1970-01-01
    • 2022-01-10
    • 1970-01-01
    • 1970-01-01
    • 2015-04-09
    • 2018-04-12
    相关资源
    最近更新 更多