并发matplotlib.pyplot Windows

时间:2016-12-19 08:45:46

标签: python matplotlib graph

当我运行此代码时,第二个matplotlib.pyplot窗口仅在我关闭第一个后才出现,当我按顺序打开它们时。如何同时显示多个窗口?

 def graph(xList, yList, string):
     xArr = numpy.array(xList)
     yArr = numpy.array(yList)
     matplotlib.pyplot.plot(xArr,yArr)
     matplotlib.pyplot.title(string)
     matplotlib.pyplot.show()



graph(posX,posY, "positive")
graph(negX,negY, "negative") 

2 个答案:

答案 0 :(得分:1)

一旦完成所有事情,您需要告诉pyplot只显示数字。 因此,根据需要创建尽可能多的数字,但最后只调用show()

import matplotlib.pyplot
import numpy
posX = numpy.arange(19)
posY = posX
negX,negY = posX*(-1), posY*(-1)


def graph(xList, yList, string):
    xArr = numpy.array(xList)
    yArr = numpy.array(yList)
    matplotlib.pyplot.figure()
    matplotlib.pyplot.plot(xArr,yArr)
    matplotlib.pyplot.title(string)

graph(posX,posY, "positive")
graph(negX,negY, "negative") 

matplotlib.pyplot.show()

答案 1 :(得分:0)

不要在您的绘图功能中包含show(),让调用代码决定何时显示绘图。

如果有的话,大多数绘图功能都会重复使用当前的数字。如果你想要一个新的数字,你必须明确地创建它。

以下代码将同时显示两个数字。注意如何在第二个绘图之前创建新图形,并且仅在完成所有绘图之后调用show()

x = numpy.arange(10)
matplotlib.pyplot.plot(x, x)
matplotlib.pyplot.figure()
matplotlib.pyplot.plot(x, x**2)
matplotlib.pyplot.show()