这是Seaborn如何制作水平箱线图的文档。您会注意到他们使用了一个名为crashes的数据集,并绘制了一些分类变量与一些数值变量之间的图形。要使它水平,他们只需翻转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'创建,因为我想象它在幕后进行了一些猜测,并且在使用2种数值类型时失败了,但是然后我遇到了TypeError: unsupported operand type(s) for /: 'str' and 'int'
orient="h"
- mwaskomdodge=False
参数。 - mwaskom