为分组列创建直方图

时间:2018-06-17 00:16:59

标签: python pandas matplotlib plot histogram

我是Python的新手。如何创建一行三列的绘图,在每列中我绘制直方图?数据来自此DataFrame:

 import pandas as pd
import matplotlib as plt
d = {'col1': ['A','A','A','A','A','A','B','B','B','B','B','B','C','C','C','C','C','C'], 
     'col2': [3, 4, 3, 4, 6, 7, 8, 9, 3, 2, 3, 4, 5, 3, 4, 1, 2, 6 ]}
df = pd.DataFrame(data=d)

在DataFrame中我们有三组(A,B,C),但我可以有N组,我仍然希望有一行一行,每列是每组的直方图。 谢谢!

3 个答案:

答案 0 :(得分:2)

您可以旋转数据框并链接绘图命令以生成图形。

import pandas as pd
import matplotlib.pyplot as plt

d = {'Category': ['A','A','A','A','A','A','B','B','B','B','B','B','C','C','C','C','C','C'], 
     'Values': [3, 4, 3, 4, 6, 7, 8, 9, 3, 2, 3, 4, 5, 3, 4, 1, 2, 2 ]}
df = pd.DataFrame(d)

df.pivot(columns='Category', values='Values').plot(kind='hist', subplots=True, rwidth=0.9, align='mid')

enter image description here

编辑:您可以使用下面的代码绘制一行中的所有子图。然而,对于三个以上的类别,情节开始看起来非常压扁。

df2 = df.pivot(columns='Category', values='Values')
color = ['blue', 'green', 'red']
idx = np.arange(1, 4)
plt.subplots(1, 3)
for i, col, colour in zip(idx, df2.columns, color):
    plt.subplot(1, 3, i)
    df2.loc[:, col].plot.hist(label=col, color=colour, range=(df['Values'].min(), df['Values'].max()), bins=11)
    plt.yticks(np.arange(3))
    plt.legend()

enter image description here

答案 1 :(得分:1)

您可以创建一行子图并用直方图填充每个子图:

import pandas as pd
from matplotlib import pyplot as plt
from matplotlib.ticker import FormatStrFormatter

#define toy dataset
d = {'col1': ['A','A','A','A','A','A','B','B','B','B','B','B','C','C','C','C','C','C'], 
     'col2': [3, 4, 3, 4, 6, 7, 8, 9, 3, 2, 3, 4, 5, 3, 4, 1, 2, 6 ]}
df = pd.DataFrame(data=d)

#number of bins for histogram
binnr = 4
#group data in dataframe
g = df.groupby("col1")
#create subplots according to unique elements in col1, same x and y scale for better comparison
fig, axes = plt.subplots(1, len(g), sharex = True, sharey = True)
#just in case you will extend it to a 2D array later
axes = axes.flatten()

#minimum and maximum value of bins to have comparable axes for all histograms
binmin = df["col2"].min()
binmax = df["col2"].max()

#fill each subplot with histogram
for i, (cat, group) in enumerate(g): 
    axes[i].set_title("graph {} showing {}".format(i, cat))
    _counts, binlimits, _patches = axes[i].hist(group["col2"], bins = binnr, range = (binmin, binmax))

#move ticks to label the bin borders
axes[0].set_xticks(binlimits)
#prevent excessively long tick labels
axes[0].xaxis.set_major_formatter(FormatStrFormatter('%0.1f'))
plt.tight_layout()
plt.show()

示例输出: enter image description here

答案 2 :(得分:0)

我认为这是您搜索的代码:

import pandas as pd
import matplotlib.pyplot as plt
d = {'col1': ['A','A','A','A','A','A','B','B','B','B','B','B','C','C','C','C','C','C'], 
     'col2': [3, 4, 3, 4, 6, 7, 8, 9, 3, 2, 3, 4, 5, 3, 4, 1, 2, 6 ]}
df = pd.DataFrame(data=d)

keys = sorted(df['col1'].unique())

vals = []
for k in keys:
    vals.append(sum(df.loc[df['col1'] == k]['col2']))

print(vals)

plt.bar(keys, vals)
plt.show()

这是你在这个例子中得到的: enter image description here

问我是否需要解释(或只是谷歌吧)。