【问题标题】:Concurrent matplotlib.pyplot Windows并发 matplotlib.pyplot 窗口
【发布时间】:2023-03-21 23:27:01
【问题描述】:

当我运行此代码时,第二个 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") 

【问题讨论】:

    标签: python matplotlib graph


    【解决方案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()
    

    【讨论】:

      【解决方案2】:

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

      大多数绘图函数将重用当前图形(如果有的话)。如果你想要一个新的人物,你必须明确地创建它。

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

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

      【讨论】:

        猜你喜欢
        • 2018-02-08
        • 2021-05-26
        • 1970-01-01
        • 2021-09-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多