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

May*_*ant 2 python matplotlib histogram pandas plotly

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

TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'
Run Code Online (Sandbox Code Playgroud)

当我使用时,我总是得到一个漂亮的直方图

px.histogram(df, x="columnName")
Run Code Online (Sandbox Code Playgroud)

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

px.histogram(df, y="columnName") or px.histogram(df, x="columnName", orientation='h') don't work. 
Run Code Online (Sandbox Code Playgroud)

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

我尝试使用go.Figure(data=[go.Histogram(y=df['columnName'])])它确实给了我一个水平历史记录,但随后我无法根据不同的列更改颜色。

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

ves*_*and 5

建议

如果你看一下下面的细节,你会发现我完全同意这有点奇怪。但如果您想确定方向,只需省略该orientation参数并在将值分配给x和之间切换即可y

水平的

fig = px.histogram(x=np.random.randn(500))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

垂直的

fig = px.histogram(y=np.random.randn(500))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

细节

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

orientation:str,'h'水平或'v'垂直之一。(默认情况下'v',如果提供xy且均为连续或均为分类,否则'v'( 'h') 如果x( y) 是分类且 y( x) 是连续的,否则'v'( 'h') 如果只有x( y) 是

但我收到这个错误:

类型错误:+ 不支持的操作数类型:“NoneType”和“str”

无论如何,px.histogram(x=np.random.randn(500))都会产生以下水平图:

在此输入图像描述

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

px.histogram(y=np.random.randn(500))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述