【问题标题】:Multiple distplots from pandas columns来自熊猫列的多个分布图
【发布时间】:2019-03-29 17:12:34
【问题描述】:

我正在尝试使用 Seaborn 绘制 Pandas DataFrame 的内容,但我一辈子都无法弄清楚如何堆叠 distplots。我拥有的 DataFrame 看起来像这样(简化)。

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

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

本质上,我想做的是:

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):Example plot


编辑

基于@con_u 的回答,我生成了以下图表:

No Zoom Zoomed In on the Origin

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

【问题讨论】:

  • 好像这个问题之前已经回答过:stackoverflow.com/questions/32899463/…。这会回答你的问题吗?
  • 感谢@HielkeWalinga,但遗憾的是这对我来说不起作用。
  • 也许尝试在一个轴上进行一些日志缩放?您能否提供代码以便我们重现您的结果?
  • 很遗憾,由于限制,我不能。对数缩放绝对是一个好主意,并且可以修复水平缩放,但它并不能解决仍然出现条形的问题。我会稍微调整一下,看看我是怎么做的。谢谢
  • 那些条似乎来自rug=True。您可以将其更改为rug=False

标签: python pandas seaborn


【解决方案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)

您可能还想在这里查看相关的:

【讨论】:

  • 谢谢。这类似于我最初尝试的方式(除了我忘记了用于重新调整列的双括号)。然而,我最终以这种方式得到了非常丑陋的情节。我在最初的问题中添加了图表的图像(由于评论限制)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-23
  • 1970-01-01
  • 2017-07-20
  • 2015-04-10
  • 2018-12-15
  • 2020-10-19
相关资源
最近更新 更多