如何在Plotly Python中解开堆放的bargraph?

发布于 2025-02-12 06:52:11 字数 465 浏览 0 评论 0原文

我正在使用PX栏图,在我的数据框架中,有重复的行将分配为X到bar绘图。 默认情况下,条形图将它们堆叠在一起,更改Barmode无济于事。 我想如下屏幕上的屏幕上分开绘制它们。 Excel会做到的方式。 先感谢您。

import plotly.express as px
abc=np.random.randint(1,30, 15)
df=pd.DataFrame(abc, columns=['v1'])
df['v2']=list('abc'*5)
fig = px.bar(df, x='v2',y='v1')
fig

I'm using px bar plot and in my dataframe there are repeating rows which will be assigned as x to bar plot.
By default bar plots stacks them, and changing barmode doesn't help.
I want to draw them seperatly as shown in screen below. The way excel would do it.
Thank you in advance.

import plotly.express as px
abc=np.random.randint(1,30, 15)
df=pd.DataFrame(abc, columns=['v1'])
df['v2']=list('abc'*5)
fig = px.bar(df, x='v2',y='v1')
fig

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

听风念你 2025-02-19 06:52:12

在这种情况下,我认为最好将X轴指定为数据框架的索引,并使用列数据更新轴标签。

import plotly.express as px
import numpy as np

abc=np.random.randint(1,30, 15)
df=pd.DataFrame(abc, columns=['v1'])
df['v2']=list('abc'*5)
fig = px.bar(df, x=df.index,y='v1')

fig.update_xaxes(title_text='v2', tickvals=df.index, ticktext=df['v2'].tolist())
fig.show()

In such a case, I think it would be better to specify the x-axis as the index of the data frame and update the axis labels with the column data.

import plotly.express as px
import numpy as np

abc=np.random.randint(1,30, 15)
df=pd.DataFrame(abc, columns=['v1'])
df['v2']=list('abc'*5)
fig = px.bar(df, x=df.index,y='v1')

fig.update_xaxes(title_text='v2', tickvals=df.index, ticktext=df['v2'].tolist())
fig.show()

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文