matplotlib 循环中的海运图

yeotifhr  于 2022-12-13  发布在  其他
关注(0)|答案(6)|浏览(99)

我正在使用Spyder并在循环中绘制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')
trnvg8h3

trnvg8h31#

你可以在每个循环中创建一个新的图形,或者在不同的坐标轴上绘图。下面是在每个循环中创建新图形的代码。它还可以更有效地获取int和float列。

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

sgtfey8w2#

在调用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);
sbtkgmzw

sbtkgmzw3#

要在评论中回答问题:如何在单个图中绘制所有内容?我还展示了一种在控制台中一个在另一个下面查看图的替代方法。

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

备注:

  • 如果列中的值范围不同-设置sharex=False或将其删除
  • 不需要标题:seborn自动将列名作为xlabel插入
  • 对于紧凑视图,将xlabels更改为ylabel,如代码片段中所示
ijnw1ujt

ijnw1ujt4#

您可以plt.show为每个迭代添加一个www.example.com(),如下所示

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()
vsdwdz23

vsdwdz235#

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()
vyswwuz2

vyswwuz26#

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)

相关问题