如何使用Seaborn绘制多列分类条形图?

时间:2019-03-07 07:51:17

标签: python-3.x pandas matplotlib plot seaborn

我有一个数据框,如下所示: enter image description here

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

enter image description here

数据为here

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

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

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)']]

1 个答案:

答案 0 :(得分:2)

您想先melt,然后使用带有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)})

输出:

enter image description here

要保存使用

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

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