所以我有这个代码:
plt.style.use('bmh')
fig = plt.figure(figsize=(10,5))
ax = fig.add_subplot(111)
ax.plot(months, monthly_profit, 'b-',lw=3)
plt.xlabel('Monthhs')
plt.ylabel('Profit')
plt.yticks(np.arange(10000,25000,step=1500))
plt.title('Profit Chart')
play = True
while play:
x = int(input("State your choise : "))
if x == 3:
plt.show()
print("Would you like to continue? YES or NO?")
y = input()
if y == "NO":
play = False
plt.close("all")
似乎它根本没有关闭情节。不是 close('all') ,也不是 close()。我想要的是能够打开它并保持打开状态,直到用户说出他的答案,然后再关闭它。有什么帮助吗?:D
您的绘图未关闭的原因是因为plt.show()
阻止了执行,因此您的代码甚至没有到达该plt.close("all")
行。要解决此问题,您可以plt.show(block=False)
在调用show
.
要重新打开绘图并让您的循环按照您的预期工作,您需要将绘图创建逻辑移到while
循环内。但是请注意,plt.style.use('bmh')
不得将其放置在此循环中。
下面是一个例子:
import matplotlib.pyplot as plt
import numpy as np
# sample data
months = [1,2,3]
monthly_profit = [10, 20, 30]
plt.style.use('bmh')
play = True
while play:
fig = plt.figure(figsize=(10,5))
ax = fig.add_subplot(111)
ax.plot(months, monthly_profit, 'b-',lw=3)
plt.xlabel('Monthhs')
plt.ylabel('Profit')
plt.yticks(np.arange(10000,25000,step=1500))
plt.title('Profit Chart')
x = int(input("State your choise : "))
if x == 3:
plt.show(block=False)
print("Would you like to continue? YES or NO?")
y = input()
if y == "NO":
play = False
plt.close("all")
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句