【问题标题】:How to plot multi column categorical bar chart using seaborn?如何使用 seaborn 绘制多列分类条形图?
【发布时间】:2019-07-29 00:48:59
【问题描述】:

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

我想以一种能够绘制条形图的方式来构建它,如下所示:

数据为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)']]

【问题讨论】:

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


【解决方案1】:

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

输出:

为了节省使用

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

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

【讨论】:

    猜你喜欢
    • 2023-03-16
    • 2021-07-28
    • 2021-12-19
    • 1970-01-01
    • 1970-01-01
    • 2019-05-29
    • 2021-06-18
    • 2021-04-26
    • 2020-11-13
    相关资源
    最近更新 更多