如何使用 Pandas DF 绘制计数条形图,按一个分类列分组并按另一个分类着色

tla*_*gan 12 python pandas

我有一个大致如下所示的数据框:

  Property   Name    industry
1  123     name1    industry 1
1  144     name1    industry 1
2  456     name2    industry 1
3  789     name3    industry 2
4  367     name4    industry 2
.  ...     ...      ... 
.  ...     ...      ... 
n  123     name1    industry 1
Run Code Online (Sandbox Code Playgroud)

我想制作一个条形图,绘制每个 Names 的行数,并根据它的行业为条形着色。我试过这样的事情:

ax = df['name'].value_counts().plot(kind='bar',
                                    figsize=(14,8),
                                    title="Number for each Owner Name")
ax.set_xlabel("Owner Names")
ax.set_ylabel("Frequency")
Run Code Online (Sandbox Code Playgroud)

我得到以下信息:

差不多好了

我的问题是如何根据数据框中的行业列为条形着色(并添加图例)。

谢谢!

tla*_*gan 10

这是我的回答:

def plot_bargraph_with_groupings(df, groupby, colourby, title, xlabel, ylabel):
    """
    Plots a dataframe showing the frequency of datapoints grouped by one column and coloured by another.
    df : dataframe
    groupby: the column to groupby
    colourby: the column to color by
    title: the graph title
    xlabel: the x label,
    ylabel: the y label
    """

    import matplotlib.patches as mpatches

    # Makes a mapping from the unique colourby column items to a random color.
    ind_col_map = {x:y for x, y in zip(df[colourby].unique(),
                               [plt.cm.Paired(np.arange(len(df[colourby].unique())))][0])}


    # Find when the indicies of the soon to be bar graphs colors.
    unique_comb = df[[groupby, colourby]].drop_duplicates()
    name_ind_map = {x:y for x, y in zip(unique_comb[groupby], unique_comb[colourby])}
    c = df[groupby].value_counts().index.map(lambda x: ind_col_map[name_ind_map[x]])

    # Makes the bargraph.
    ax = df[groupby].value_counts().plot(kind='bar',
                                         figsize=FIG_SIZE,
                                         title=title,
                                         color=[c.values])
    # Makes a legend using the ind_col_map
    legend_list = []
    for key in ind_col_map.keys():
        legend_list.append(mpatches.Patch(color=ind_col_map[key], label=key))

    # display the graph.
    plt.legend(handles=legend_list)
    ax.set_xlabel(xlabel)
    ax.set_ylabel(ylabel)
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明


Yil*_*ang 7

这可能有点太复杂了,但这确实有效。我首先定义了从名称到行业以及从行业到颜色的映射(似乎只有两个行业,但您可以根据自己的情况调整字典):

ind_col_map = {
    "industry1": "red",
    "industry2": "blue"
}

unique_comb = df[["Name","industry"]].drop_duplicates()
name_ind_map = {x:y for x, y in zip(unique_comb["Name"],unique_comb["industry"])}
Run Code Online (Sandbox Code Playgroud)

然后就可以使用上面的两个映射来生成颜色了:

c = df['Name'].value_counts().index.map(lambda x: ind_col_map[name_ind_map[x]])
Run Code Online (Sandbox Code Playgroud)

最后,您只需要简单地添加color到绘图函数中:

ax = df['Name'].value_counts().plot(kind='bar',
                                    figsize=(14,8),
                                    title="Number for each Owner Name", color=c)
ax.set_xlabel("Owner Names")
ax.set_ylabel("Frequency")
plt.show()
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明


Sco*_*ton 6

让我们使用一些数据帧整形和 matplotlib:

ax = df.groupby(['industry','Name'])['Name'].count().unstack(0).plot.bar(title="Number for each Owner Name", figsize=(14,8))
_ = ax.set_xlabel('Owner')
_ = ax.set_ylabel('Frequency')
Run Code Online (Sandbox Code Playgroud)

输出:

在此处输入图片说明


小智 6

使用 seaborn.countplot

import seaborn as sns
sns.set(style="darkgrid")
titanic = sns.load_dataset("titanic")
ax = sns.countplot(x="class", data=titanic)
Run Code Online (Sandbox Code Playgroud)

参考 seaborn https://seaborn.pydata.org/generated/seaborn.countplot.html的文档