【发布时间】:2021-06-20 03:20:43
【问题描述】:
我希望将这两个箱形图组合成一张图片: [![这两个数据文件让我可以轻松使用 Seaborn 箱线图制作箱线图][1]][1]
我使用的数据文件来自多个 Excel 电子表格,如下所示:
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | ... |
|---|---|---|---|---|---|---|---|
| 5 | 2 | 3 | 5 | 6 | 2 | 5 | ... |
| 2 | 3 | 4 | 6 | 1 | 2 | 1 | ... |
| 1 | 2 | 4 | 6 | 7 | 8 | 9 | ... |
| ... | ... | ... | ... | ... | ... | ... | ... |
列标题代表小时,列值是我想用来创建箱线图和晶须图的值。
目前我的代码是这样的:
import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt
xls = pd.ExcelFile('ControlDayVar.xlsx')
df1= pd.read_excel(xls, 'DE_ControlDays').assign(Location=1)
df2= pd.read_excel(xls, 'DE_FestDays').assign(Location=2)
DE_all =pd.concat([df1,df2])
DE= pd.melt(DE_all, id_vars=['Location'], var_name=['Hours'], value_name='Concentration')
ax= sns.boxplot(x='Hours', y= 'Concentration', hue= 'Location', data=DE)
plt.show()
The result I get is this:
[![Yikes][2]][2]
I expect my issue has to do with the format of my data files, but any help would be appreciated.Thanks!
[1]: https://i.stack.imgur.com/dXo6F.jpg
[2]: https://i.stack.imgur.com/NEpi7.jpg
【问题讨论】:
-
要检查数据集是否符合预期的格式,你能显示
DE.head()的结果吗? -
感谢您的建议!查看 DE.head(),格式看起来是正确的,浓度、时间和位置都被准确表示。 imgur.com/lLFnnDA
-
您确定该列被识别为数值数据类型吗?
-
你完全正确!由于数据文件中存在一些挥之不去的字符,浓度值被视为字符串。快速修复,情节看起来很棒。非常感谢!
-
太棒了!别客气。那我就给个正经的回答吧。