[英]Plotting box plots of two columns side by side in seaborn
I would like to plot two columns of a pandas dataframe as side by side box plots by category. 我想将熊猫数据框的两列绘制为按类别并排的箱形图。 This is not the same as the question presented in here: Grouped boxplot with seaborn where the two columns have lists inside them.
这与此处提出的问题不同: 带seaborn的分组箱线图,其中两列内部都有列表。 The solution there did not work for me.
那里的解决方案对我不起作用。
import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame(
[
[2, 4, "A"],
[4, 5, "C"],
[5, 4, "B"],
[10, 4.2, "A"],
[9, 3, "B"],
[3, 3, "C"]
], columns=['data1', 'data2', 'Categories'])
#Plotting by seaborn
fig, axs = plt.subplots(1, 1)
sns.boxplot(data=df,x="Categories",y='data1',ax=axs)
fig.show()
plt.waitforbuttonpress()
plt.close(fig)
The code above generates: 上面的代码生成:
Replacing "data1" with "data2" in the boxplot line would give: 在箱线图行中将“ data1”替换为“ data2”将得出:
You need to melt
(convert to long format) the DataFrame
first: 您需要首先
melt
(转换为长格式) DataFrame
:
data = df.melt(id_vars=['Categories'], var_name='dataset', value_name='values')
print(data)
Prints: 打印:
Categories dataset values
0 A data1 2.0
1 A data2 4.0
2 C data1 4.0
3 C data2 5.0
4 B data1 5.0
5 B data2 4.0
6 A data1 10.0
7 A data2 4.2
8 B data1 9.0
9 B data2 3.0
10 C data1 3.0
11 C data2 3.0
Now you just have to use dataset
as the hue. 现在,您只需要使用
dataset
作为色调。 Since the plot is quite busy I moved the legend outside it. 由于剧情非常繁忙,我将图例移到了剧情之外。
sns.boxplot(data=data, x='Categories', y='values', hue='dataset')
plt.legend(title='dataset', loc='upper left', bbox_to_anchor=(1, 1))
Edit by OP : 由OP编辑 :
I implemented this in a function such that it makes the plot with as many columns as desired in an ax and returns it. 我在一个函数中实现了它,使得它可以在斧头中根据需要创建具有多列的图并返回它。
def box_plot_columns(df,categories_column,list_of_columns,legend_title,y_axis_title,**boxplotkwargs):
columns = [categories_column] + list_of_columns
newdf = df[columns].copy()
data = newdf.melt(id_vars=[categories_column], var_name=legend_title, value_name=y_axis_title)
return sns.boxplot(data=data, x=categories_column, y=y_axis_title, hue=legend_title, **boxplotkwargs)
Usage Example: 用法示例:
fig, ax = plt.subplots(1,1)
ax = box_plot_columns(Data,"Categories",["data1","data2"],"dataset","values",ax=ax)
ax.set_title("My Plot")
plt.show()
Try This : 尝试这个 :
df = pd.DataFrame(
[
[2, 4, "A"],
[4, 5, "C"],
[5, 4, "B"],
[10, 4.2, "A"],
[9, 3, "B"],
[3, 3, "C"]
], columns=['data1', 'data2', 'Categories'])
#Plotting by seaborn
df_c = pd.melt(df, "Categories", var_name="data1", value_name="data2")
sns.factorplot("Categories",hue="data1", y="data2", data=df_c, kind="box")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.