Python 从列中提取多个图
我试图使用Seaborn来绘制熊猫数据帧的内容,但我一生都搞不清楚如何堆叠这些图。我拥有的数据帧看起来像这样(简化) 现在,我要做的是绘制N个对象在整个图像集中的分布。有了这些,我想看看有多少图像中有Obj1,有多少图像中有Obj2,等等。这是一个纯视觉的东西,所以不要想这可能不是显示上述数据的最佳方式 本质上,我想做的是:Python 从列中提取多个图,python,pandas,seaborn,Python,Pandas,Seaborn,我试图使用Seaborn来绘制熊猫数据帧的内容,但我一生都搞不清楚如何堆叠这些图。我拥有的数据帧看起来像这样(简化) 现在,我要做的是绘制N个对象在整个图像集中的分布。有了这些,我想看看有多少图像中有Obj1,有多少图像中有Obj2,等等。这是一个纯视觉的东西,所以不要想这可能不是显示上述数据的最佳方式 本质上,我想做的是: for column in df.column: sns.distplot(column) # Stack these distributions together
for column in df.column:
sns.distplot(column) # Stack these distributions together with different colors
plt.show() # Display one plot with N-distribution plots inside
希望获得类似的输出(ish):
编辑 基于@con_的回答,我生成了以下图: 它们是相当无用的,尽管在图1中可以看到垂直条。我知道分布严重偏向较低的数字(计数),所以可能我运气不好,需要重新考虑我的绘图选项。这对我很有用
# ----------------------------------------------------------------------
# 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)
您还可以在此处查看相关信息:
似乎这个问题以前已经得到了回答:。这能回答你的问题吗?谢谢@HielkeWalinga,但遗憾的是,这在我的情况下不起作用。也许可以尝试在axis上进行一些日志缩放?你能提供代码让我们重现你的结果吗?遗憾的是,由于限制,我不能。日志缩放肯定是一个好主意,可以修复水平缩放,但它不能解决条形图仍然显示的问题。我会稍微调整一下,看看我是怎么做的。感谢这些条似乎来自
rug=True
。您可以将其更改为rug=False
。谢谢。这与我最初尝试的方式类似(只是我忘记了用于参照柱的双括号)。然而,我却以这样丑陋的情节收场。我在最初的问题中添加了情节的图像(由于评论的限制)。
# ----------------------------------------------------------------------
# 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)