【问题标题】:How to plot multiple bar charts in python [duplicate]如何在python中绘制多个条形图[重复]
【发布时间】:2019-07-27 21:40:44
【问题描述】:

我的意图是使用 matplotlib 和 seaborn 重新创建下图:

问题是,我这样做的方式只获得了用于中介的图表。我当前的图表如下所示:

我的代码如下所示:

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[
        ['Message Size (Bytes)', 'Concurrent Users', '90th Percentile of Response Time (ms)', '95th Percentile of Response Time (ms)',
         '99th Percentile of Response Time (ms)']]

    df_results = df_results.set_index(['Message Size (Bytes)', 'Concurrent Users']).stack().reset_index().rename(
        columns={'level_2': 'Summary', 0: 'Response Time (ms)'})

    sns.barplot(x='Concurrent Users', y='Response Time (ms)', hue='Summary', data=df_results, ci=None)
    ax.yaxis.set_major_formatter(tkr.FuncFormatter(lambda y, p: "{:,}".format(y)))
    plt.suptitle(title)
    plt.legend(loc=2, frameon=True, title="Response Time Summary")
    plt.show()
    plt.savefig(filename)
    plt.clf()
    plt.close(fig)

数据如下:

link 包含数据

【问题讨论】:

    标签: pandas matplotlib plot bar-chart seaborn


    【解决方案1】:

    你可以使用 melt 重塑你的 DataFrame

    df_ = df[['Concurrent Users', '90th Percentile of Response Time (ms)',
         '95th Percentile of Response Time (ms)', '98th Percentile of Response Time (ms)',
         '99th Percentile of Response Time (ms)', '99.9th Percentile of Response Time (ms)']].melt('Concurrent Users')
    

    并在barplot 中使用hue 参数

    fig, ax = plt.subplots()
    sns.barplot(x='Concurrent Users', y='value', hue=0, data=df_, ax=ax)
    

    新的 DataFrame df_ 如下所示:

       Concurrent Users                                      0  value
    0                50  90th Percentile of Response Time (ms)     26
    1               100  90th Percentile of Response Time (ms)     51
    2               200  90th Percentile of Response Time (ms)    105
    3               300  90th Percentile of Response Time (ms)    158
    4               500  90th Percentile of Response Time (ms)    243
    

    还有这些dtypes

    Concurrent Users     int64
    0                   object
    value                int64
    

    【讨论】:

    • 不幸的是,这在执行 line 时出错:sns.barplot(x='Concurrent Users', y='value', hue=0, data=df_, ax=ax)。错误提示:TypeError: 'int' object is not iterable
    • df_.dtypes 的输出是什么?
    • 我做了:df_results = df_results[ ['并发用户','第 90 个响应时间百分比(毫秒)','第 95 个响应时间百分比(毫秒)','第 99 个响应时间百分比( ms)']].melt('Concurrent Users') 并将数据类型指定为系列,但是当我将结果分配给“df_”时,它表示对象
    • 抱歉,您没有回答我的问题,df_ 不能是Series
    • 对不起,它说的是 DataFrame
    【解决方案2】:

    你可以试试这个:

    df_out = df.set_index(['Concurrent Users','Scenario Name']).filter(like='Percentile').unstack()
    df_out.columns = [f'{i} - {j}' for i, j in df_out.columns]
    df_out.plot.bar(figsize=(15,10))
    

    输出:

    【讨论】:

      猜你喜欢
      • 2018-05-20
      • 2020-09-15
      • 1970-01-01
      • 1970-01-01
      • 2019-03-02
      • 2016-07-06
      • 1970-01-01
      • 2022-01-07
      • 2020-07-22
      相关资源
      最近更新 更多