【问题标题】:Bar chart with bars from two different dataframes来自两个不同数据框的条形图
【发布时间】:2020-09-19 20:37:59
【问题描述】:

我有以下数据框:

      import pandas as pd
      import numpy as np
      import matplotlib.pyplot as plt

      df_One = pd.DataFrame({'Category': ['1024Sen', '1024Act', '2089Eng', '2089Sen'], 
                             'Qtd_Instrumentation': [18, 5, 25, 10]})


     df_Two = pd.DataFrame({'Category': ['1024Sen', '1024Act', '2089Eng', '2089Sen'], 
                   'Qtd_Instrumentation': [14, 1, 22, 10]})

我想构建一个包含两个数据框信息的条形图,即蓝色条表示datadrame_One,垂直红色条表示dataframe_Two的信息。

我尝试如下实现:

       fig, ax = plt.subplots()
       n_group = len(df_One['Category'])
       index = np.arange(n_group)
       bar_width = 0.35
       opacity = 0.8


       rects1 = df_One.plot.bar(x='Category', y='Qtd_Instrumentation', color='r', label = 'Station 
                                One')
       rects2 = df_Two.plot.bar(x='Category', y='Qtd_Instrumentation', color='b', label = 'Station 
                                Two')


       plt.xlabel('Category Instrumentation')
       plt.ylabel('Qtd Instrumentation')

       plt.show()

但是,此代码是错误的,因为它设计了两个条形图,而不是仅构建一个具有两种表示形式的图。

有谁知道我如何构建这个描述的图表?提示

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    这是一种方法,选择align='edge' 选项,然后对一个条使用正宽度,对另一个使用负宽度。这将使它们彼此对齐。另外,您必须致电plt.legend() 才能显示图例

    fig, ax = plt.subplots()
    
    index = np.arange(len(df_One['Category']))
    bar_width = 0.35
    opacity = 0.8
    
    ax.bar(index, df_One['Qtd_Instrumentation'], color='r', align='edge', width=bar_width, label = 'Station One')
    ax.bar(index, df_Two['Qtd_Instrumentation'], color='b', align='edge', width=-bar_width,label = 'Station Two')
    
    # Assign the tick labels
    ax.set_xticks(index)
    ax.set_xticklabels(df_One['Category'], rotation=90)
    
    plt.xlabel('Category Instrumentation')
    plt.ylabel('Qtd Instrumentation')
    plt.legend()
    plt.show()
    

    替代方法如下,使用关键字position 将条形图彼此相邻放置,如图所示here

    df_One.Qtd_Instrumentation.plot(kind='bar', color='red', ax=ax, width=bar_width, position=1)
    df_Two.Qtd_Instrumentation.plot(kind='bar', color='blue', ax=ax, width=bar_width, position=0)
    
    ax.set_xlim(-0.5, 3.5)
    ax.set_xticks(index)
    ax.set_xticklabels(df_One['Category'])
    

    【讨论】:

    • 完美运行。我还有一个问题:在完整的数据框中,我有 15 个不同的类别。当我设计图表时,名称重叠。你知道如何解决这个问题吗?
    • @JaneBorges :在设置刻度标签时使用rotation = 90
    • 好主意!这样,类别是垂直的。还有一个问题:我尝试使用以下命令增加图表:ax.plot (kind = 'bar', figsize = (15.10))。然而,它并没有增加。你知道如何放大图表吗?
    • @JaneBorges : 使用fig, ax = plt.subplots(figsize = (15, 10)) 然后其余代码保持不变
    • 可能需要一个逗号,例如figsize=(15, 10)
    【解决方案2】:

    我建议先合并两个数据框:

    df_c = pd.merge(df_One, df_Two, on='Category')
    df_c.plot.bar(x='Category')
    

    给予:

    请注意,如果您缺少类别,您可能需要传递 how='outer' 来合并。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-07-20
      • 1970-01-01
      • 2016-12-05
      • 1970-01-01
      • 2017-03-11
      • 2019-02-11
      • 1970-01-01
      相关资源
      最近更新 更多