慕姐4208626
将数据帧转换为长格式.stack()绘制和sns.boxplot使用hue='gender'Seaborn是一个基于matplotlib.import pandas as pdimport matplotlib.pyplot as pltimport seaborn as sns# datadata = {'gender': [0, 0, 0, 1, 1], 'amb1_1': [15.0, 16.0, 17.0, 18.0, 19.0], 'intel1_1': [25.0, 26.0, 27.0, 28.0, 29.0], 'attr1_1': [15.0, 16.0, 17.0, 18.0, 19.0], 'sinc1_1': [25.0, 26.0, 27.0, 28.0, 29.0], 'shar1_1': [15.0, 16.0, 17.0, 18.0, 19.0]}# dataframedf = pd.DataFrame(data)# convert to longdfl = df.set_index('gender').stack().reset_index().rename(columns={'level_1': 'groups', 0: 'values'})# plotp = sns.boxplot(x='groups', y='values', data=dfl, hue='gender')plt.show()和plotly数据帧需要很长,就像dfl上一个例子使用选择计算四分位数的算法中的示例代码import plotly.express as pxfrom plotly.offline import plotfig = px.box(dfl, x="groups", y="values", color="gender")plot(fig)