简体   繁体   English

seaborn 中的水平条形图,带有数字独立数据

[英]Horizontal barplot in seaborn with numeric independent data

This is the Seaborn Doc on how to make a horizontal boxplot.这是有关如何制作水平箱线图的 Seaborn 文档。 You'll notice that they use some dataset called crashes and are graphing some categorical variable vs some numeric variable.您会注意到他们使用了一些称为崩溃的数据集,并且正在绘制一些分类变量与一些数值变量的图表。 To make it horizontal, they are just flipping the x and y variable, easy enough.为了使其水平,他们只是翻转 x 和 y 变量,很容易。

My issue is that my categories are numeric which causes problems.我的问题是我的类别是数字,这会导致问题。 A minimum reproducible example uses their dataset.一个最小的可重现示例使用他们的数据集。 Basically this should be the same graph one horizontal and the other vertical.基本上这应该是一个水平和另一个垂直的相同图表。 As you can see they are both vertical...如您所见,它们都是垂直的...

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)

在此处输入图像描述

I tried making the column 'roundTotal' of type string as I imagined that it was doing some guessing under the hood and was failing with the 2 numeric types, but then I run into TypeError: unsupported operand type(s) for /: 'str' and 'int'我尝试制作字符串类型的“roundTotal”列,因为我想象它在后台进行了一些猜测并且在 2 种数字类型上失败了,但后来我遇到了TypeError: unsupported operand type(s) for /: 'str' and 'int'

Just add this before plotting:只需在绘图之前添加:

crashesMod.roundTotal=crashesMod.roundTotal.astype('category')
crashesMod.VsAverage=crashesMod.VsAverage.astype('category')

According the the seaborn docs, you should use the 'orient' option:根据 seaborn 文档,您应该使用“orient”选项:

import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
sns.set(style="whitegrid")

fig, ax = plt.subplots(figsize=(3.5, 6))

crashes = (
    sns.load_dataset("car_crashes")
        .sort_values("total", ascending=False)
        .assign(roundTotal=lambda df: df['total'].round().astype(int).astype('category'))
        .groupby(['roundTotal']).count()
        .reset_index()
        .assign(VsAverage=lambda df: df['total'] > df['total'].mean())
        .pipe((sns.barplot, 'data'), y='roundTotal', x='total',
              hue='VsAverage', orient='horizonal', ax=ax)
)

ax.invert_yaxis()

And that gives me:这给了我:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM