[英]Why won't my grouped box plot work in Python?
我有一个看起来像这样的数据集(my_data):
Gender Time Money Score
Female 23 14 26.74
Male 12 98 56.76
Male 11 34 53.98
Female 18 58 25.98
etc.
我想根据分数制作一个性别分组框 plot,这样在同一张图中就会有两个图。
到目前为止我的代码:
Males = [my_data.loc[my_data['Gender']=='Male', 'Score']]
Females = [my_data.loc[my_data['Gender']=='Female', 'Score']]
Score = [Males, Females]
fig, ax = plt.subplots()
ax.boxplot(Score)
plt.show()
但是,这会运行一条错误消息:
ValueError:X 必须有 2 个或更少的维度
我尝试将 Males 和 Females 转换为一个数组,认为 Python 可能不喜欢它作为一个列表:
Males = np.array([my_data.loc[my_data['Gender']=='Male', 'Score']])
Females = np.array([my_data.loc[my_data['Gender']=='Female', 'Score']])
但这仍然没有用。 Python 也确实说它将列表作为箱线图的值,所以我无论如何都不需要这样做。
我还尝试了另一种制作箱线图的方法:
fig = plt.figure(figsize =(10, 7))
ax = fig.add_axes([Males, Females])
bp = ax.boxplot(Score)
plt.show()
它给了我这个错误代码:
TypeError: ufunc 'isfinite' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
<Figure size 720x504 with 0 Axes>
这是怎么回事?
提取源数据时,将不需要的数据放在方括号中。
将它们生成为:
Males = my_data.loc[my_data['Gender']=='Male', 'Score']
Females = my_data.loc[my_data['Gender']=='Female', 'Score']
然后,要生成您的盒子 plot,您可以运行例如:
fig, ax = plt.subplots(1, 1)
ax.boxplot([Males, Females])
ax.set_xticklabels(['Males', 'Females'])
plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.