How to plot multi column categorical bar chart usi

2020-05-08 11:43发布

问题:

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:

The data is here.

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

回答1:

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)})

Output:

To save use

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

And just wrap all that in a function.