Tommy
Tommy

Reputation: 131

prevent matplotlib output in jupyter notebook (python)

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

Upvotes: 0

Views: 472

Answers (2)

Ray Walker
Ray Walker

Reputation: 293

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.

Upvotes: 0

Tommy
Tommy

Reputation: 131

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

Upvotes: 2

Related Questions