Tronald Dump
Tronald Dump

Reputation: 1350

Seaborn plots in a loop

I am using Spyder and plotting Seaborn countplots in a loop. The problem is that the plots seem to be happening on top of each other in the same object and I end up seeing only the last instance of the plot. How can I view each plot in my console one below the other?

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')        

Upvotes: 47

Views: 93639

Answers (6)

jashia
jashia

Reputation: 1

import seaborn as sns
import matplotlib.pyplot as plt




def print_heatmap(data):
    plt.figure()
    sns.heatmap(data)

for i in data. columns :
    print_heatmap(i)

Upvotes: -1

Vignesh V
Vignesh V

Reputation: 1

plt.figure(figsize= (20,7))
for i,col in enumerate(signals.columns[:-1]):
 k = i +1
 plt.subplot(2,6,int(k))
 sns.distplot(x= signals[col], 
 color='darkblue',kde=False).set(title=f"{col}");
 plt.xlabel("")
 plt.ylabel("")
 #plt.grid()
plt.show()

Upvotes: 0

Andres Perez
Andres Perez

Reputation: 3

You can add a plt.show() for each iteration as follows

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')
       plt.show()   

Upvotes: 0

Ted Petrou
Ted Petrou

Reputation: 61967

You can create a new figure each loop or possibly plot on a different axis. Here is code that creates the new figure each loop. It also grabs the int and float columns more efficiently.

import matplotlib.pyplot as plt

df1 = df.select_dtypes([np.int, np.float])
for i, col in enumerate(df1.columns):
    plt.figure(i)
    sns.countplot(x=col, data=df1)

Upvotes: 45

sherdim
sherdim

Reputation: 1236

To answer on the question in comments: How to plot everything in the single figure? I also show an alternative method to view plots in a console one below the other.

import matplotlib.pyplot as plt

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

n=len(df1.columns)
fig,ax = plt.subplots(n,1, figsize=(6,n*2), sharex=True)
for i in range(n):
    plt.sca(ax[i])
    col = df1.columns[i]
    sns.countplot(df1[col].values)
    ylabel(col);

Notes:

  • if a range of values in your columns differs - set sharex=False or remove it
  • no need for titles: seaborn automatically inserts column names as xlabel
  • for compact view change xlabels to ylabel as in the code snippet

Upvotes: 7

jorgeh
jorgeh

Reputation: 1767

Before calling sns.countplot you need to create a new figure.

Assuming you have imported import matplotlib.pyplot as plt you can simply add plt.figure() right before sns.countplot(...)

For example:

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);

Upvotes: 31

Related Questions