Pandas列的多个距离图

2024-09-28 22:35:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图使用Seaborn来绘制Pandas数据帧的内容,但就我的一生而言,我无法想出如何堆叠distplots。我的数据帧看起来像这样(简化)。在

Image    | Obj1 | Obj2 | ... | ObjN
-----------------------------------
img1.jpg |   2  |   1  | ... | 0
-----------------------------------
img2.jpg |   5  |   5  | ... |  5
-----------------------------------
img3.jpg |   9  |   0  | ... |  1

现在,我要做的是绘制N个对象在整个图像集上的分布。我想看看有多少图像中有Obj1,有多少图像有Obj2,等等。这是一个纯粹的视觉问题,所以不要想这可能不是显示所述数据的最佳方式。在

本质上,我想做的是:

^{pr2}$

希望得到类似于(ish)的输出:Example plot


编辑

根据@con_u u的回答,我生成了以下图表:

No ZoomZoomed In on the Origin

尽管在图1中可以看到垂直条,但它们是相当无用的。我知道分布严重偏向于较低的数字(计数),所以也许我运气不好,需要重新考虑我的绘图选项。在


Tags: 数据图像image内容pandas绘制seabornjpg
1条回答
网友
1楼 · 发布于 2024-09-28 22:35:06

对我有用。在

#                                    
# Import library
#                                    
import numpy as np
import pandas as pd
import seaborn as sns
import random

#                                    
# Create an artificial dataframe
#                                    
df = pd.DataFrame({'image':np.arange(100) + 1,
                  'obj1':np.random.randint(low=1, high=100, size=100),
                  'obj2':np.random.randint(low=1, high=100, size=100),
                  'obj3':np.random.randint(low=1, high=100, size=100),
                  'obj4':np.random.randint(low=1, high=100, size=100)})

df = df.set_index('image')
df.head(10)

#                                    
# Plot the distributions (column-wise) (if you just want some columns)
#                                    
sns.distplot(df[['obj1']], hist=False, rug=True)
sns.distplot(df[['obj2']], hist=False, rug=True)
sns.distplot(df[['obj3']], hist=False, rug=True)
sns.distplot(df[['obj4']], hist=False, rug=True)
sns.plt.show()

#                                    
# Plot the distributions (column-wise) (looping method)
#                                    
for col in df.columns:
  sns.distplot(df[[col]], hist=False, rug=True)

您还可以在此处查看相关内容:

enter image description here

相关问题 更多 >