如何使用Seaborn绘制多列分类条形图? [英] How to plot multi column categorical bar chart using seaborn?

查看:1300
本文介绍了如何使用Seaborn绘制多列分类条形图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据框,如下所示:

I have a data frame as shown below:

我希望以某种方式构造它,以便能够绘制如下所示的条形图:

I want to structure it in a way that I will be able to plot a bar chart as shown below:

数据位于此处.

注意:Echo API数据=中介数据

Note : Echo API data = Mediation data

我现有的代码如下所示,我不知道如何进行.非常感谢您的帮助.

My existing code is shown below, I do not know how to proceed with it. Any help is much appreciated.

def save_bar_chart(title):
    filename = "response_time_summary_" + str(message_size) + "_" + str(backend_delay) + "ms.png"
    print("Creating chart: " + title + ", File name: " + filename)
    fig, ax = plt.subplots()
    fig.set_size_inches(11, 8)

    df_results = df.loc[(df['Message Size (Bytes)'] == message_size) & (df['Back-end Service Delay (ms)'] == backend_delay)]

    df_results = df_results[
        [ 'Scenario Name','Concurrent Users', '90th Percentile of Response Time (ms)', '95th Percentile of Response Time (ms)',
         '99th Percentile of Response Time (ms)']]

推荐答案

您要melt,然后使用带有hue的条形图:

You want to melt and then use a barplot with hue:

import seaborn as sns

small_data = df_results[[ 'Scenario Name','Concurrent Users', '90th Percentile of Response Time (ms)', 
                 '95th Percentile of Response Time (ms)','99th Percentile of Response Time (ms)']]
small_data = small_data.melt(id_vars=['Scenario Name', 'Concurrent Users'])
small_data['new_var'] = small_data.variable + ' - ' + small_data['Scenario Name']

g = sns.barplot(x="Concurrent Users", y="value", hue='new_var', data=small_data)
sns.set(rc={'figure.figsize':(11,8)})

输出:

要保存使用

fig = g.get_figure()
fig.savefig(filename)

然后将所有内容包装在一个函数中.

And just wrap all that in a function.

这篇关于如何使用Seaborn绘制多列分类条形图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆