3

Code:

import matplotlib.pyplot as plt
from time import sleep
for i in range(100):
    plt.pie([100-i,i])
    sleep(1)
    plt.show()

enter image description here

Problem with my code:

  • Whenever the values of plt.pie() changes and the pie chart are plotted then execution of for loop seems to be halted and I have to close the pie chart window to resume to the execution of for loop.

What I want to do:

  • plt.pie() display live changes in its values without halting for loop's execution.

thank you.

1
  • 1
    Please have a look at matplotlib animations, which are especially created to be able to run code inside the event loop and to hence avoid such problems. Commented Oct 12, 2018 at 16:24

2 Answers 2

1

add plt.close() to your for-loop

import matplotlib.pyplot as plt
from time import sleep
for i in range(100):
    plt.pie([100-i,i])
    sleep(1)
    plt.show()
    plt.close()
Sign up to request clarification or add additional context in comments.

3 Comments

This is not working. plt.show() seems to be blocking the execution of plt.close() function too.
what if you remove plt.show() and add plt.figure() as the first line in your for loop?
I used plt.draw() with plt.pause() and it is working now. plt.draw() is a non-blocking function so it does not halts the execution of the loop.
0

Fixed it.

import matplotlib.pyplot as plt
from time import sleep
for i in range(100):
   plt.pie([100-i, i])
   plt.pause(.001)
   plt.draw()
   sleep(1)

plt.show() is blocking function so instead used plt.draw() along with plt.pause() and now it's working as intended.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.