【问题标题】:Python - Seaborn "Dodged" Barch Chart from DataframePython - 来自 Dataframe 的 Seaborn “Dodged” Barch 图
【发布时间】:2020-04-23 16:42:00
【问题描述】:

我正在尝试使用 Seaborn 从名为“航空公司”的以下数据框中绘制一个闪避的条形图:

    Airline_Name    Count   Total_Negative_Reviews  Total_Neutral_Reviews   Total_Positive_Reviews
0   American        2604    1864                    433                     307
1   Delta           2222    955                     723                     544
2   Southwest       2420    1186                    664                     570
3   US Airways      2913    2263                    381                     269
4   United          3822    2633                    697                     492
5   Virgin America  504     181                     171                     152

我的目标是为每家航空公司设置总共 4 个条形图:一个代表评论总量,一个代表负面评价,另一个代表中立,另一个代表正面评价。

到目前为止,我可以绘制一个包含总金额的图表(如下所示),但我似乎无法弄清楚如何添加其他 3 个。

我已经拥有的代码(及其可视化):

sns.barplot(data = airlines, x = airlines["Airline_Name"], y = airlines["Count"])

enter image description here

有人可以帮我解决这个问题吗?

【问题讨论】:

    标签: python data-visualization seaborn


    【解决方案1】:

    需要使用melt() 进行小型预处理才能在seaborn 中制作这样的图:

    df1 = df.melt(id_vars='Airline_Name', var_name='variable', value_name='values')
    sns.barplot(x='Airline_Name',y='value',hue='variable',data=df1)
    plt.show()
    

    输出

    如果使用pandas 绘图是一个选项,您可以通过以下方式获得所需的绘图:

    import pandas as pd
    import matplotlib.pyplot as plt
    
    #df = pd.read_csv('airline.csv')
    df.plot.bar(x='Airline_Name',stacked=False,subplots=False, figsize=(12,7))
    plt.tight_layout()
    plt.show()
    

    输出

    如果你想拥有seaborn 风格和pandas 情节,你可以执行以下操作:

    import pandas as pd
    import matplotlib.pyplot as plt
    
    sns.set_style('darkgrid')
    #df = pd.read_csv('airline.csv')
    
    #if you want a stacked bar plot, keep stacked=True
    df.plot.bar(x='Airline_Name',stacked=False,subplots=False, figsize=(12,7))
    plt.tight_layout()
    plt.show()
    

    输出

    【讨论】:

      【解决方案2】:

      堆积条形图(水平或垂直)可能最好地描述您的数据。

      我使用 matplotlib 快速而肮脏地完成了这项工作,但这个概念与 seaborn 相似,因为它在很大程度上基于 matplotlib。这个概念是您将矩形设置在彼此之上。其他库(例如 Plotly、Bokeh、Altair、Vega 等)可能还有其他方法可以做到这一点,所以请不要把这个答案当成福音。

      您也可以通过 pandas 直接使用分组 DataFrame 执行此操作,但我时间紧迫,希望其他人可以将答案添加到此特定线程。

      完成你想要的代码是:

      import pandas as pd
      import numpy as np
      import matplotlib.pyplot as plt
      
      # convert the columns to a numpy array
      a_count = airlines['Count'].to_list()
      a_neg = np.array(airlines['Total_Negative_Reviews'].to_list())
      a_neut = np.array(airlines['Total_Neutral_Reviews'].to_list())
      a_pos = np.array(airlines['Total_Positive_Reviews'].to_list())
      a_name = airlines['Airline_Name'].to_list()
      
      # set the width of the bars
      width = 0.5
      
      # create the plot
      fig, ax = plt.subplots()
      ax.bar(a_name, a_neg, width, label="Negative")
      ax.bar(a_name, a_neut, width, bottom=a_neg, label='Neutral')
      ax.bar(a_name, a_pos, width, bottom=a_neg+a_neut, label='Positive')
      
      
      ax.legend()
      ax.set_ylabel('# of Reviews')
      ax.set_xlabel('Airline')
      plt.show()
      

      这将产生这个图表:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-09-26
        • 2020-08-13
        • 2018-02-18
        • 2021-11-01
        • 2016-11-03
        • 2022-01-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多