Python:使用与结果相同的变量绘制多条线
Python: plot multiple lines with the same variable as outcome
为了学校,我必须制作一个程序来绘制您需要掷多少次骰子才能购买所有东西,每次使用不同数量的现金。用于计算掷骰子等的脚本工作正常,但我不知道如何制作情节。这是我计算不同金额起始现金的代码:
money = [2500, 2000, 1500, 1000, 500, 0]
count = 0
for value in money:
while True:
throw()
piece.move(distance)
count = count + 1
start()
buy()
if (poslen) == 36:
print("It took", count,"rolls.")
plt()
break
之后我想画一个如下图:
def plt():
money = [0,500,1000,1500,2000,2500]
line0 = [count]
line500 = [count]
line1000 = [count]
line1500 = [count]
line2000 = [count]
line2500 = [count]
plt.xlabel("Amount of money")
plt.ylabel("Amount of throws")
plt.title("Throws with varying amounts of money")
plt.plot(money, line0)
plt.plot(money, line500)
plt.plot(money, line1000)
plt.plot(money, line1500)
plt.plot(money, line2000)
plt.plot(money, line2500)
plt.show()
变量计数当然每行都不同(因为你需要更多轮次来买东西)。所以我的问题是如何为每一行打印正确的 Y 值?(我希望我的问题不会含糊)
据称已经有效的代码不完整:缺少 poslen
、throw
、start
、buy
和 distance
,所以我可以确切地确定你在那里做什么,但看起来你的循环结构不必要地复杂。改成这个怎么样?
for value in money:
while not poslen == 36:
throw()
piece.move(distance)
count = count + 1
start()
buy()
print("Het duurde", count, "worpen voordat je alles had.")
plt()
它也很可疑 value
没有在任何地方使用。这些谜团无法帮助我们理解您的代码应该实现什么。
至于您的 plt
函数,我建议您将各种计数存储在某个容器中。应该是词典吧我不清楚你是如何生成数据的,但如果你设法将它们放入字典中(我们称之为 line
),plt
可能看起来像这样:
def plt():
for n, data in line.items():
plt.plot(money, data, label=str(n))
plt.xlabel("Amount of money")
plt.ylabel("Amount of throws")
plt.title("Throws with varying amounts of money")
为了学校,我必须制作一个程序来绘制您需要掷多少次骰子才能购买所有东西,每次使用不同数量的现金。用于计算掷骰子等的脚本工作正常,但我不知道如何制作情节。这是我计算不同金额起始现金的代码:
money = [2500, 2000, 1500, 1000, 500, 0]
count = 0
for value in money:
while True:
throw()
piece.move(distance)
count = count + 1
start()
buy()
if (poslen) == 36:
print("It took", count,"rolls.")
plt()
break
之后我想画一个如下图:
def plt():
money = [0,500,1000,1500,2000,2500]
line0 = [count]
line500 = [count]
line1000 = [count]
line1500 = [count]
line2000 = [count]
line2500 = [count]
plt.xlabel("Amount of money")
plt.ylabel("Amount of throws")
plt.title("Throws with varying amounts of money")
plt.plot(money, line0)
plt.plot(money, line500)
plt.plot(money, line1000)
plt.plot(money, line1500)
plt.plot(money, line2000)
plt.plot(money, line2500)
plt.show()
变量计数当然每行都不同(因为你需要更多轮次来买东西)。所以我的问题是如何为每一行打印正确的 Y 值?(我希望我的问题不会含糊)
据称已经有效的代码不完整:缺少 poslen
、throw
、start
、buy
和 distance
,所以我可以确切地确定你在那里做什么,但看起来你的循环结构不必要地复杂。改成这个怎么样?
for value in money:
while not poslen == 36:
throw()
piece.move(distance)
count = count + 1
start()
buy()
print("Het duurde", count, "worpen voordat je alles had.")
plt()
它也很可疑 value
没有在任何地方使用。这些谜团无法帮助我们理解您的代码应该实现什么。
至于您的 plt
函数,我建议您将各种计数存储在某个容器中。应该是词典吧我不清楚你是如何生成数据的,但如果你设法将它们放入字典中(我们称之为 line
),plt
可能看起来像这样:
def plt():
for n, data in line.items():
plt.plot(money, data, label=str(n))
plt.xlabel("Amount of money")
plt.ylabel("Amount of throws")
plt.title("Throws with varying amounts of money")