2015-05-09 70 views
0

我有一個循環來生成大熊貓DF每列的圖。我使用Ipython,但是這些圖都顯示在循環結尾,而不是顯示在我想要根據代碼顯示它們的地方。ipython強制大熊貓繪製

我該如何強制ipython/pandas在我有'plot'功能的精確點上顯示cols?

def explore(file, sep=";", top = 5, k='Code Agence'): 
    """ 

    """ 
    %matplotlib inline 
    import time 
    import matplotlib.pyplot as plt 
    import pandas as pd 
    import time 
    import sys 
    dataframes_top = [] 
    start = time.time() 
    #print "Exploring :", get_file_name(file), "with %s lines"%(top) 

    to_explore = pd.read_csv(file, sep=";", error_bad_lines=False) 
    cols = to_explore.columns 
    i = -1 
    for col in cols: 
     i +=1 
     serie = to_explore[col] 
     try: 
      print"plotting %s"%(col) 
      serie.plot().show() 
      time.sleep(2) 
     except Exception as e: 
      "plotting issue :%s"%(e) 
     #serie.index = index 

     null = serie.isnull() 
     not_null = len([x for x in null if not x]) 
     r = not_null/len(serie) 

     s = serie.value_counts()#return value as index, count as value 
     pct_top = s.values[:top]/not_null 
     serie_top_n = pd.Series(s.values[:top],index=s.index[:top]) 
     local_df = pd.DataFrame() 
     local_df[col]=serie_top_n 
     local_df['pct']=pct_top 
     somme = local_df['pct'].sum() 

     pct_2_top= s.values[:top*2]/not_null 
     serie_2_top_n = pd.Series(s.values[:top*2],index=s.index[:top*2]) 
     local_df_2_top = pd.DataFrame() 
     local_df_2_top[col]=serie_2_top_n 
     local_df_2_top['pct']=pct_2_top 
     somme_2_top = local_df_2_top['pct'].sum() 


     print 
     print "%s : [col %s = %s ] "%(get_file_name(file), i,col) 
     print 
     print "%.2f"%(r), " pct not null" 
     print "%.2f pct on the first %s "%(somme, top) 
     print "%.2f pct on the first %s "%(somme_2_top, 2*top) 
     print "plot :" 
     print pd.DataFrame(serie.describe()).T 

     print 
     print local_df.T 
     print "plot :" 
     local_df.plot() 

     print "="*100 

     dataframes_top.append(local_df) 
    elapsed = time.time()-start 
    print "="*20, elapsed, "for %s lines"%(len(serie)),"="*20 
    sys.stdout.flush() 

回答

0

每次繪製新圖時務必打電話給plt.show()。如果您不這樣做,iPython會自動緩衝每個繪圖並在到達單元結束時顯示它們。我想你會在循環結束時忘記這麼做。

下面是一些代碼,將正確地繪製的環內的曲線,而不是朝向非常端等待一個例子:

%matplotlib inline 

import matplotlib.pyplot as plt 
import random 
from pandas import Series 
from numpy.random import randn 

for i in range(5): 
    print("Before graph {0}".format(i)) 
    ts = Series(randn(1000), index=date_range('1/1/2000', periods=1000)) 
    ts = ts.cumsum() 
    ts.plot() 

    plt.show() 
    print("After graph {0}".format(i)) 

如果我運行此,被打印的輸出之間顯示的每個情節,如期望。

我使用IPython筆記本版本3.0.0-f75fda4使用Python 3.