Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Close a figure - PyCharm

I have spent over an hour searching, just to figure this simple thing. So, before considering this a duplicate question, please compare my question to any question out there.

This is my code:

import pandas
import matplotlib.pyplot as plt

dataset = pandas.read_csv('international-airline-passengers.csv', usecols=[1], engine='python', skipfooter=1)
print dataset, type(dataset)
plt.plot(dataset)
plt.show()
plt.close()

Firstly, plt.show() to my understanding is a blocking function. So what is the way to close the figure. There is no point in writing plt.close() after it. So where is the right way to put it.

Secondly, how can I make sure all the windows are closed when I execute a new process of the same python code. For example in MATLAB, one could easily say close all in the beginning of their file and it closes all the opened plots which were the result of previous MATLAB code execution. plt.close('all') is not working either.

I am using PyCharm. The results I found for the first situation, might work on IDLE but not in the PyCharm. How can I do it PyCharm.

like image 350
Jesh Kundem Avatar asked Nov 04 '16 01:11

Jesh Kundem


People also ask

Can PyCharm show plots?

Interactive plots in the SciView window are not supported by PyCharm at the moment. If you want to use the mouse cursor to see the values at specific graph points, you can render plots in the native matplotlib GUI.


2 Answers

plt.show(block=False) will do the trick- This is the way you can make this function non-blocking (both in run & debug mode). The main dis-advantage is that if the code ends, the figure is automatically closes...

like image 66
YoniChechik Avatar answered Sep 20 '22 07:09

YoniChechik


I had the same problem. I fix it making the python file in Pycharm to run in only one console. Go to: run---Edit configuration-- check that "single instance only" is activated

like image 41
Rida Aboulhaich Avatar answered Sep 24 '22 07:09

Rida Aboulhaich