2016-12-25 68 views
3

我使用Spyder並在循環中繪製Seaborn countplots。問題在於情節似乎在同一個對象中彼此重疊,我最終只能看到情節的最後一個實例。我怎樣才能在我的控制檯中查看每個圖形?Seaborn地塊在一個循環中

for col in df.columns: 
    if ((df[col].dtype == np.float64) | (df[col].dtype == np.int64)): 
     i=0 
     #Later 
    else : 
     print(col +' count plot \n') 
     sns.countplot(x =col, data =df) 
     sns.plt.title(col +' count plot')   

回答

4

在致電sns.countplot之前,您需要創建一個新圖。

假設您已經導入import matplotlib.pyplot as plt你可以簡單地以前sns.countplot(...)

例如右加plt.figure()

import matplotlib 
import matplotlib.pyplot as plt 
import seaborn 

for x in some_list: 
    df = create_df_with(x) 
    plt.figure() #this creates a new figure on which your plot will appear 
    seaborn.countplot(use_df); 
7

您可以創建一個新的圖中,每個迴路或可能在不同軸線上繪製。這是每個循環創建新數字的代碼。它也更有效地抓取int和float列。

df1 = df.select_dtypes([np.int, np.float]) 

for i, col in enumerate(df1.columns): 
    plt.figure(i) 
    sns.countplot(x=col, data=df1)