【问题标题】:Seaborn plots kdeplot but not distplotSeaborn 绘制 kdeplot 但不绘制 distplot
【发布时间】:2016-05-25 05:08:08
【问题描述】:

我想绘制从 couchdb 形成的 pandas 数据框列中的数据。这是数据的代码和输出:

print df4.Patient_Age

Doc_ID
000103f8-7f48-4afd-b532-8e6c1028d965    99
00021ec5-9945-47f7-bfda-59cf8918f10b    92
0002510f-fb89-11e3-a6eb-742f68319ca7    32
00025550-9a97-44a4-84d9-1f6f7741f973    73
0002d1b8-b576-4db7-af55-b3f26f7ca63d    49
0002d40f-2b45-11e3-8f66-742f68319ca7    42
000307eb-18a6-47cd-bb03-33e484fad029    18
00033d3d-1345-4739-9522-b41b8db3ee23    42
00036d2e-0a51-4cfb-93d1-3e137a026f19    42
0003b054-5f3b-4553-8104-f71d7a940d84    10
Name: Patient_Age, dtype: object

如果我执行这段代码:

sns.kdeplot(df4.Patient_Age)

情节按预期生成。但是,当我运行它时:

sns.distplot(df4.Patient_Age)

distplot 出现以下错误:

TypeError: unsupported operand type(s) for /: 'unicode' and 'long'

为了纠正错误,我使用:

df4.Patient_Age = [int(i) for i in df4.Patient_Age]
all(isinstance(item,int) for item in df4.Patient_Age)

输出是:

False

我想了解的是:

  1. 为什么更早生成了 kdeplot 而不是 histplot?
  2. 当我将数据类型更改为int 时,为什么我仍然得到False?而如果数据不是int(如False所示),为什么转换后histplot会起作用?

【问题讨论】:

标签: python pandas matplotlib analytics seaborn


【解决方案1】:

问题是您的值不是数字。如果您将它们强制为整数或浮点数,它将起作用。

from io import StringIO

import pandas
import seaborn
seaborn.set(style='ticks')

data = StringIO("""\
Doc_ID                                  Age 
000103f8-7f48-4afd-b532-8e6c1028d965    99
00021ec5-9945-47f7-bfda-59cf8918f10b    92
0002510f-fb89-11e3-a6eb-742f68319ca7    32
00025550-9a97-44a4-84d9-1f6f7741f973    73
0002d1b8-b576-4db7-af55-b3f26f7ca63d    49
0002d40f-2b45-11e3-8f66-742f68319ca7    42
000307eb-18a6-47cd-bb03-33e484fad029    18
00033d3d-1345-4739-9522-b41b8db3ee23    42
00036d2e-0a51-4cfb-93d1-3e137a026f19    42
0003b054-5f3b-4553-8104-f71d7a940d84    10
""")

df = pandas.read_table(data, sep='\s+')
df['Age'] = df['Age'].astype(float)

df.info()
# prints

<class 'pandas.core.frame.DataFrame'>
Int64Index: 10 entries, 0 to 9
Data columns (total 2 columns):
Doc_ID    10 non-null object
Age       10 non-null float64
dtypes: float64(1), object(1)
memory usage: 240.0+ bytes

那么:

seaborn.distplot(df['Age'])

给我:

【讨论】:

    猜你喜欢
    • 2019-06-03
    • 2016-12-30
    • 2020-09-26
    • 2020-12-02
    • 2021-11-03
    • 2021-04-06
    • 1970-01-01
    • 2016-11-28
    • 2020-11-14
    相关资源
    最近更新 更多