这是有关如何制作水平箱线图的Seaborn文档。您会注意到,他们使用了一些称为崩溃的数据集,并绘制了一些分类变量与一些数字变量的图表。为了使其水平,他们只是简单地翻转x和y变量。
我的问题是我的类别是数字,这会引起问题。一个最小的可重现示例使用其数据集。基本上,这应该是同一图,一个水平,另一个垂直。如您所见,它们都是垂直的...
import seaborn as sns
import matplotlib.pyplot as plt
sns.set(style="whitegrid")
crashes = sns.load_dataset("car_crashes").sort_values("total", ascending=False)
crashes['roundTotal'] = np.round(crashes['total']).astype(int)
crashesMod = crashes.groupby(['roundTotal']).count().reset_index()
crashesMod['VsAverage'] = crashesMod['total'] > crashesMod.total.mean()
sns.barplot(x = 'roundTotal', y = 'total', hue = 'VsAverage', data = crashesMod)
plt.show()
sns.barplot(y = 'roundTotal', x = 'total', hue = 'VsAverage', data = crashesMod)
我试着将字符串类型的列“ roundTotal”设置为字符串,因为我想它在幕后做了一些猜测,但使用了两个数字类型却失败了,但是随后我遇到了 TypeError: unsupported operand type(s) for /: 'str' and 'int'
只需在绘制之前添加以下内容:
crashesMod.roundTotal=crashesMod.roundTotal.astype('category')
crashesMod.VsAverage=crashesMod.VsAverage.astype('category')
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句