如何使用 Python 在 Plotly Express 中制作水平直方图?

How do I make a horizontal histogram in Plotly express using Python?

我正在尝试用我的数据制作水平直方图,但出现此错误:

TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'

当我使用

时,我总是得到一个漂亮的直方图
px.histogram(df, x="columnName")

但是当我尝试将其更改为水平时,它不起作用。

px.histogram(df, y="columnName") or px.histogram(df, x="columnName", orientation='h') don't work. 

我没有使用 NoneType 的数据,我什至尝试了 px.histogram(y=np.random.randn(500)) 但它仍然不起作用。

我尝试使用 go.Figure(data=[go.Histogram(y=df['columnName'])]),它确实给我一个水平直方图,但是我无法根据不同的列更改颜色。

任何帮助将不胜感激,提前致谢:)

建议

如果您查看下面的详细信息,您会发现我完全同意这有点奇怪。但是,如果您想确定方向,只需省略 orientation 参数并在将值分配给 xy 之间切换。

水平

fig = px.histogram(x=np.random.randn(500))

垂直

fig = px.histogram(y=np.random.randn(500))

详情

这整件事看起来有点奇怪。 orientation 被列为 px.histogram 的参数, 应该 'h''v' 作为有效参数。

orientation: str, one of 'h' for horizontal or 'v' for vertical. (default 'v' if x and y are provided and both continous or both categorical, otherwise 'v'('h') if x(y) is categorical and y(x) is continuous, otherwise 'v'('h') if only x(y) is

但是我收到这个错误:

TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'

在任何情况下,px.histogram(x=np.random.randn(500)) 都会生成以下水平图:

如果您想将其翻转为垂直,只需交换 x with y:

px.histogram(y=np.random.randn(500))