Akhmad Zaki
Akhmad Zaki

Reputation: 433

Python Spyder IDE - Plot to File Only

I want to save a plot created using matplotlib to a file but I do not want it to show as inline plot in Spyder IDE. My code:

import matplotlib.pyplot as plt
from math import sin,pi
import numpy as np

x = np.linspace(0,2*pi,100)
y = np.sin(x)

plt.plot(x,y)
plt.savefig('sin.png')

When I run this code, the plot keep showing in IPython console as inline plot whereas I just want to save it to a file. How can I fix this problem?

Upvotes: 0

Views: 1802

Answers (2)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339660

This behaviour is steered by some of the settings in spyder.

First, you may of course opt not to use IPython at all. Instead if the script is executed in a new python console, it will not pop up at all without you specifying plt.show() at the end.

enter image description here

If however you want to use IPython, but not get any graphical output, you may deactivate the graphical output for the IPython console. I.e. no checkmark at "Activate support". This will then also require you to call plt.show() to actually show the figure in a new window.

enter image description here

Note that changing those settings will require to restart Spyder.

Those are the general settings. If you want this behaviour only for a single script, use plt.close() at the end of a cell/script.

Upvotes: -1

HYRY
HYRY

Reputation: 97331

add plt.close() after plt.savefig().

Upvotes: 2

Related Questions