0

The program gets stuck at the graph even though i use plt.show(block=False). I want to be able to close the graph and after x amount of time and plot something else but it doesn't work.

Sorry about this I have now uploaded the code needed for it to run.

import matplotlib.pyplot as plt import time

class Point: def init(self, x, y): self.x = x self.y = y

def plot(self):
    fig = plt.scatter(self.x, self.y)

def __add__(self, other):
    if isinstance(other, Point):
        x = self.x + other.x
        y = self.y + other.y
        return Point(x, y)
    else:
        x = self.x + other
        y = self.y + other
        return Point(x, y)

def main():
    a = Point(1, 1)
    b = Point(2, 2)
    c = a + b
    e = Point(0, 2)
    d = e + 5
    f = a + Point(1, 1)
    listAZ = [a, b, c, d, e, f]

    for i in listAZ:
        i.plot()
    plt.show(block=False)
    plt.show()
    time.sleep(5)
    plt.close()

The only thing is i need the program to continue executing after this line. I want the plot the graph then pause a bit and continue with the rest of the code.

Indentation is correct. There is no problem with that.

Pantzaris
  • 49
  • 8

1 Answers1

0

The code is paused because of the plt.show() just after the plt.show(block=False). You need to remove it in order to effectively continue the execution while the plot is shown.

But it leads to another problem: removing this line will likely lead to a frozen window as the plot does not have time to be drawn before the code continue. For that, you have to add a short plt.pause just after the plt.show(block=False).

The following should work:

def main():
    a = Point(1, 1)
    b = Point(2, 2)
    c = a + b
    e = Point(0, 2)
    d = e + 5
    f = a + Point(1, 1)
    listAZ = [a, b, c, d, e, f]
    for i in listAZ:
        i.plot()
    plt.show(block=False)
    plt.pause(0.01)
    time.sleep(5)
    plt.close()
leleogere
  • 908
  • 2
  • 15