0

I tried to render several hundred images with python and matplotlib using the following code:

def render_state(np_data_array):
    filename = "render/fig_" + str(i) + ".png"

    fig = plt.figure(figsize = (12,12) )
    aa = fig.add_subplot(111)
    aa.imshow(np_data_array,cmap='gray')

    fig.savefig(filename)

unfortunately matplotlib is also rendering everything into the jupyter notebook. Is there a way to prevent any output from matplotlib? I couldn't really find a way and most answers on the internet just say to not use plt.show() what I don't even use

4
  • If you don't have to render any plot at all in a notebook, don't call %matplotlib inline? Commented Dec 18, 2017 at 17:09
  • 1
    To prevent any output from a jupyter notebook cell, use %%capture as first line of the cell. Is that what you're after? Commented Dec 18, 2017 at 17:09
  • Possible duplicate of matplotlib python inline on/off Commented Dec 18, 2017 at 17:09
  • %%capture works like charm! Thanks for the quick answer Commented Dec 18, 2017 at 17:19

2 Answers 2

2

%%capture as the first line of the cell works like charm! Thanks to ImportanceOfBeingErnest

Sign up to request clarification or add additional context in comments.

1 Comment

It's always a good idea to allow time for the commenter, who provided you with solution, to post it as answer, and then you accept it.
0

You can also just use plt.close() at the end of your notebook cell like explained here. This way, you can still render other widgets like a tqdm progress bar. With %%capture, all widgets are blocked.

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.