Is it possible to have multiple PyPlot windows? Or am I limited to subplots?

时光怂恿深爱的人放手 提交于 2019-12-28 04:00:10

问题


I'm not sure how to word my question more clearly. Basically, is PyPlot limited to one instance/window? Any hack or workaround I try either causes my program to freeze or for the second pyplot window to be queued until the first one is closed.


回答1:


Sure, just open a new figure:

import matplotlib.pyplot as plt

plt.plot(range(10))

plt.figure()
plt.plot(range(10), 'ro-')

plt.figure(), plt.plot(...)

plt.show() # only do this once, at the end

If you're running this in the default python interpreter, this won't work, as each figure needs to enter the gui's mainloop. If you want to run things in an interactive shell, look into IPython. If you just run this normally (i.e. put it into a file and call python filename.py) it will work fine, though.




回答2:


Use plt.figure() and use a certain number so that the window is fixed:

plt.figure(200)
plt.plot(x)
plt.show()

and for another plot, use a different number:

plt.figure(300)
plt.plot(y)
plt.show()



回答3:


The answer to your question is no. You can have as many windows as you want. Firstly, just type

plt.figure(n) #n must be a different integer for every window

for every new figure you want. Secondly, write

plt.show()

only once (!) at the end of everything you want to plot. Here is an example for two histograms:

plt.figure(1)
plt.hist(dataset1)
plt.figure(2)
plt.hist(dataset2)
plt.show()


来源:https://stackoverflow.com/questions/5993206/is-it-possible-to-have-multiple-pyplot-windows-or-am-i-limited-to-subplots

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!