简体   繁体   English

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

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

I'm trying to make a horizontal histogram with my data but I'm getting this error:我正在尝试用我的数据制作水平直方图,但出现此错误:

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

I always get a beautiful histogram when I use当我使用时,我总是得到一个漂亮的直方图

px.histogram(df, x="columnName")

But the moment I try to change it to horizontal, it doesn't work.但是当我尝试将其更改为水平时,它不起作用。

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

I have no data with NoneType and I even tried px.histogram(y=np.random.randn(500)) but it still doesn't work.我没有使用 NoneType 的数据,我什至尝试过px.histogram(y=np.random.randn(500))但它仍然不起作用。

I tried using go.Figure(data=[go.Histogram(y=df['columnName'])]) which does give me a horizontal hist but then I'm unable to change the colors according to a different column.我尝试使用go.Figure(data=[go.Histogram(y=df['columnName'])])确实给了我一个水平的历史,但是我无法根据不同的列更改 colors。

Any help would be appreciated, thanks in advance:)任何帮助将不胜感激,在此先感谢:)

Suggestion建议

If you take a look at the details below, you'll see that I fully agree that this is a little strange.如果你看看下面的细节,你会发现我完全同意这有点奇怪。 But if you'd like to determine the orientation, just leave out the orientation parameter and swith between assigning the values to x and y .但是,如果您想确定方向,只需省略orientation参数并在将值分配给xy之间切换。

Horizontal水平的

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

在此处输入图像描述

Vertical垂直的

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

在此处输入图像描述

Details细节

This whole thing seems a bit strange.这整件事似乎有点奇怪。 orientation is listed as a parameter for px.histogram and should take either 'h' or 'v' as valid arguments. orientation被列为px.histogram的参数,并且应该'h''v'作为有效的 arguments。

orientation: str, one of 'h' for horizontal or 'v' for vertical.方向:str,水平方向的'h'或垂直方向'v'之一。 (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 (如果提供了xy并且都是连续的或都是分类的,则默认为'v' ,否则,如果x ( y ) 是分类的并且y ( x ) 是连续的,则为 'v 'v' (' 'h' '),否则为'v''h' ) 如果只有x ( y ) 是

But I'm getting this error:但我收到了这个错误:

TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'类型错误:+ 不支持的操作数类型:“NoneType”和“str”

In any case, px.histogram(x=np.random.randn(500)) produces the following horizontal plot plot:在任何情况下, px.histogram(x=np.random.randn(500))产生以下水平 plot plot:

在此处输入图像描述

If you'd like to flip it to vertical, just exchange x with y :如果您想将其翻转为垂直,只需将x with y交换:

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

在此处输入图像描述

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

相关问题 如何使用 Plotly Python 对事件的直方图进行动画处理? - How can I animate a histogram of occurrences using Plotly Python? 如何删除 plotly express 图形对象中的网格线(python) - How do I remove gridlines in plotly express graph objects (python) 我如何使用 Plotly express 制作一维直方图而不为每一行的相同值创建新的 x 值? - How can i use Plotly express to make a 1D histogram without making new x values for each row of the same value? 如何获得可绘制的直方图中的垃圾箱数量? - How do I get the number of bins in a plotly histogram? 如何使用 python matplotlib 绘制正态分布的直方图? - How do I draw a histogram for a normal distribution using python matplotlib? Plotly:如何制作 3D 堆叠直方图? - Plotly: How to make a 3D stacked histogram? Dash Plotly-如何使2个条件绘制直方图? - Dash Plotly - How to make 2 conditions to plot a histogram? 水平色阶 Plotly Express - Horizontal colorscale Plotly Express Plotly:如何在 plotly 快递图上添加水平滚动条? - Plotly: How to add a horizontal scrollbar to a plotly express figure? 如何使用Python和pyplot将标准直方图数据绘制为极坐标直方图? - How do I plot a standard histogram data to a polar histogram using Python and pyplot?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM