Python 带ion()的MatPlotLib不显示窗口
如果我运行以下代码:Python 带ion()的MatPlotLib不显示窗口,python,matplotlib,Python,Matplotlib,如果我运行以下代码: import matplotlib.pyplot as plt import numpy as np #plt.ion() while True: print('loop') x = range(10) y = np.random.rand(10) plt.scatter(x, y) plt.show() 然后我看到一个散点图显示在我的屏幕上。然后每次关闭绘图窗口时,它都会显示一个包含新数据的新绘图 但是,如果我取消对plt.i
import matplotlib.pyplot as plt
import numpy as np
#plt.ion()
while True:
print('loop')
x = range(10)
y = np.random.rand(10)
plt.scatter(x, y)
plt.show()
然后我看到一个散点图显示在我的屏幕上。然后每次关闭绘图窗口时,它都会显示一个包含新数据的新绘图
但是,如果我取消对plt.ion行的注释,则不会显示任何内容。没有创建窗口,程序只是继续循环,打印出“循环”
我希望能够显示一个图形,然后自动返回代码,图形仍然显示。如何进行此操作?如果您希望在同一图形窗口上绘图,而不是在每次迭代时生成新窗口,则以下操作将起作用:
import matplotlib.pyplot as plt
import numpy as np
plt.ion()
fig, ax = plt.subplots(1, 1)
while True:
# If wanting to see an "animation" of points added, add a pause to allow the plotting to take place
plt.pause(1)
x = range(10)
y = np.random.rand(10)
ax.scatter(x, y)
您看到的结果将取决于所使用的matplotlib后端。如果您希望看到添加的新点,您应该使用Qt4或Qt5如何计划打破无限while循环?