Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

plot in Pandas immediately closes

I have a problem of plotting the data. I run the following python code:

import pandas as pd
df = pd.read_csv("table.csv")

values = df["blah"]
values.plot()
print 1

df['blahblah'].plot()
print 2

My output is:

50728417:Desktop evgeniy$ python test2.py
1
2
50728417:Desktop evgeniy$ 

And finally I see how launches python icon( picture of rocket, by the way , what is it?) in my Dock( using mac os), and then it disappears. Printed numbers 1,2 show me that no error exist. So have no idea what to do next.

like image 219
Ievgenii Avatar asked Oct 11 '15 07:10

Ievgenii


1 Answers

The problem is that pandas's DataFrame.plot does not block. Therefore the figures are closed when your program ends. Pandas uses matplotlib internally so you can circumvent this by calling matplotlib.pyplot.show with block=True like so:

import pandas as pd
import matplotlib.pyplot as plt

df = pd.read_csv("table.csv")

values = df["blah"]
values.plot()
print 1

df['blahblah'].plot()
print 2
plt.show(block=True)

This way the program will only end when plt.show returns, which is after you closed all figures.

like image 141
swenzel Avatar answered Oct 26 '22 23:10

swenzel