使用python按类别绘制直方图

时间:2018-04-16 23:37:57

标签: python pandas matplotlib

我有一个包含2列的表(比如说)

column A = {0,0,1,1,0,2,0,2,2,1,2,1}
column B = {0.1,0.2,0.3,0.4,0.2,0.3,0.5,0.75,0.8,0.9,1.3,0.2}

我想在列B中为A中的特定值绘制值的直方图。例如:仅在A = 0时绘制单个直方图。

我尝试过在哪里可以使用group by来绘制多个直方图。

x=test['<column header a>']
y=test['<column header b>']
df['N'].hist(by=df['column header'])

我想限制一个直方图。

1 个答案:

答案 0 :(得分:0)

首先过滤A,然后调用hist()

 import pandas as pd
 df = pd.DataFrame({'A': [0,0,1,1,0,2,0,2,2,1,2,1], 'B': [0.1,0.2,0.3,0.4,0.2,0.3,0.5,0.75,0.8,0.9,1.3,0.2]})

df[df['A']==0].hist('B')

enter image description here