Python matplotlib:在新窗口中为后续程序运行生成新图形

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31686530/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 10:25:26  来源:igfitidea点击:

matplotlib: generate a new graph in a new window for subsequent program runs

pythonmatplotlibplot

提问by Syd

I have a Python program that generates graphs using matplotlib. I am trying to get the program to generate a bunch of plots in one program run (the user is asked if they want to generate another graph) all in separate windows. Any way I can do this?

我有一个使用 matplotlib 生成图形的 Python 程序。我试图让程序在一个程序运行中生成一堆图(询问用户是否要生成另一个图形),所有这些都在单独的窗口中。我有什么办法可以做到这一点?

回答by Will Norvelle

Use the .figure()function to create a new window, the following code makes two windows:

使用该.figure()函数新建一个窗口,以下代码制作两个窗口:

import matplotlib.pyplot as plt

plt.plot(range(10))  # Creates the plot.  No need to save the current figure.
plt.draw()  # Draws, but does not block

plt.figure()  # New window, if needed.  No need to save it, as pyplot uses the concept of current figure
plt.plot(range(10, 20))
plt.draw()

You can repeat this as many times as you want

您可以根据需要多次重复此操作

回答by Zach Fox

To generate a new figure, you can add plt.figure() before any plotting that your program does.

要生成新图形,您可以在程序执行任何绘图之前添加 plt.figure()。

import matplotlib.pyplot as plt
import numpy as np

def make_plot(slope):
    x = np.arange(1,10)
    y = slope*x+3
    plt.figure()
    plt.plot(x,y)

make_plot(2)
make_plot(3)

回答by tacaswell

The easiest way to ensure all of your lines go to the correct figure window is something like:

确保所有行都进入正确的图形窗口的最简单方法是:

from six.moves import input
import matplotlib.pyplot as plt
another = True
while another:
    fig, ax = plt.subplots()

    ax.plot(range(5))


    fig.canvas.manager.show() 
    # this makes sure that the gui window gets shown
    # if this is needed depends on rcparams, this is just to be safe
    fig.canvas.flush_events() 
    # this make sure that if the event loop integration is not 
    # set up by the gui framework the plot will update

    another = bool(input("would you like another? "))

If you want to run this with a non-gui backend you will need to drop the flush_eventscall or wrap it in a try: ... except NotImplementedError. Much of this complication is defensive programming because GUIs can be difficult and the behavior of this code may be dependent on many factors which are not obvious from the code shown.

如果您想使用非 gui 后端运行它,您将需要挂断flush_events电话或将其包装在try: ... except NotImplementedError. 这种复杂性的大部分是防御性编程,因为 GUI 可能很困难,并且此代码的​​行为可能取决于许多因素,这些因素在所示代码中并不明显。

Using the implicit axes of pyplotcan cause problems as the 'current axes' is set by the last axes the user clicked on. You should really only use pyplotwhen interactively typing at the rpel and almost never (other than plt.subplots) in scripts/programs.

使用 的隐式轴pyplot可能会导致问题,因为“当前轴”是由用户单击的最后一个轴设置的。你真的应该只pyplot在 rpel 交互式输入时使用,几乎从不(除了plt.subplots)在脚本/程序中使用。

回答by driedler

Using the latest matlibplot, I found the following to work for my purposes:

使用最新的 matlibplot,我发现以下内容适用于我的目的:

# create figure (will only create new window if needed)
plt.figure()
# Generate plot1
plt.plot(range(10, 20))
# Show the plot in non-blocking mode
plt.show(block=False)

# create figure (will only create new window if needed)
plt.figure()
# Generate plot2
plt.plot(range(10, 20))
# Show the plot in non-blocking mode
plt.show(block=False)

...

# Finally block main thread until all plots are closed
plt.show()