Python-Seaborn“;回避;数据帧中的Barch图

Python-Seaborn“;回避;数据帧中的Barch图,python,data-visualization,seaborn,Python,Data Visualization,Seaborn,我正试图用Seaborn从以下名为“airlines”的数据框绘制一个闪避条形图: Airline_Name Count Total_Negative_Reviews Total_Neutral_Reviews Total_Positive_Reviews 0 American 2604 1864 433 307 1 Delta 2222 9

我正试图用Seaborn从以下名为“airlines”的数据框绘制一个闪避条形图:

    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个

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


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

您的数据可能最好用堆叠条形图(水平或垂直)来描述

我使用matplotlib快速而肮脏地完成了这项工作,但这个概念与seaborn类似,因为它主要基于matplotlib。其概念是将矩形设置在彼此的顶部。对于其他库(例如Plotly、Bokeh、Altair、Vega等),可能还有其他方法可以做到这一点,因此请不要将此答案视为福音

您也可以通过pandas直接使用分组数据帧来实现这一点,但我时间紧迫,希望其他人可以将这个答案添加到这个特定的线程中

实现您想要的功能的代码是:

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()
这将导致以下图表:

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()
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()
输出

如果您想将
seaborn
样式与
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()
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()
输出