【问题标题】:How to get multiple figures without closing first one in matplotlib如何在不关闭matplotlib中的第一个数字的情况下获得多个数字
【发布时间】:2018-08-10 15:12:27
【问题描述】:

如何在不关闭matplotlib中的第一个的情况下获得许多数字。 我的代码如下:

import matplotlib.pyplot as plt
x = [1,2,3,4,5,6,7,8,9,10]
y = [1,4,9,16,25,36,49,64,81,100]
z=[1,3,4,5,19,13,17,12,15,10]
v=[1,2,4,5,7,8,90,3,2,2]
def func(x,y):
    plt.plot(x, y)# I want the first graph to stay and get second one without closing first
    plt.show()
    return
func(x,y)
myinput=int(input())
if myinput==1:
   func(x,z)
else:
    func(x,v)
#something like this i want

【问题讨论】:

  • stackoverflow.com/a/33050617。试试plt.ion()
  • 你能帮帮我吗,我每次都想要单独的数字而不关闭第一个数字
  • 如果你不在交互模式下使用 MPL,它们会阻塞直到关闭。 plt.ion 是一个全局设置,但 block=False 是一个似乎对您有用的按数字选择 - 解决它做得很好

标签: python-2.7 matplotlib


【解决方案1】:

作为@noman 答案的缩短版:

plt.show(block=False)

例如,我这样使用它从一个脚本生成两个图,而无需关闭第一个图:

# first plot
fig = plt.figure()
plt.scatter(.....whatever....)
plt.show(block=False)

# second plot
fig = plt.figure()
plt.line(.....whatever....)
plt.show()

【讨论】:

    【解决方案2】:
    #answering myself after spending lot of time
    x = [1,2,3,4,5,6,7,8,9,10]
    y = [1,4,9,16,25,36,49,64,81,100]
    z=[1,3,4,5,19,13,17,12,15,10]
    v=[1,2,4,5,7,8,90,3,2,2]
    def func(x,y,index):
        fig=plt.figure(index)
        fig1=fig.add_subplot(1,1,1)
        fig1.plot(x, y)
        plt.show(block=False)
        return
    my_input=1
    while(my_input!=-1):
        my_input=int(input())
        func(x,y,my_input)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-12-08
      • 2016-08-09
      • 2010-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多