基于第二列的固定顺序绘制的块状barchar

发布于 2025-01-28 08:51:23 字数 1092 浏览 2 评论 0 原文

我有一个看起来像这样的数据框:

index, start, end, bar_len,name,color, gr
1,2300000.0,5300000.0,3000000.0,p36.32,#949494, g1
2, 5300000.0,7100000.0,1800000.0,p36.31,#FFFFFF,  g1
3, 7100000.0,9100000.0,2000000.0,p36.23,#949494, g1
4, 9100000.0,12500000.0,3400000.0,p36.22,#FFFFFF, g1

我想创建一个具有以下输出的水平堆叠式barchar:

| -INDX [1] [LEN = BAR_LEN] | -Indx [2] [Len = Bar_len] | -INDX [3] [len = bar_len] | -Indx [4] [Len = Bar_len]

我尝试这样做以下方法:

import plotly.express as px
import pandas as pd

input_path = r"example.csv"
df = pd.read_csv(input_path)
df.set_index('start')
fig = px.bar(
    df, x='bar_len', y='gr', color="DS_COLOR", orientation='h',
)

fig.update_layout(barmode='stack', xaxis={'categoryorder':'category ascending'})

问题是,在Barchar上绘制的值不是通过Start列对其进行排序的,这是我要做的。因此,我的问题是:是否有任何方法可以根据一列( bar_len )绘制每个元素的长度,并根据另一列( bar_len )进行分类( bar_len )( 开始)?

更新:我已经看到包括颜色标签在内时问题会引起。此标签基于颜色求和Barchart,而不是根据索引列保留原始顺序。有什么办法避免这种情况吗?

I have a dataframe that looks like this:

index, start, end, bar_len,name,color, gr
1,2300000.0,5300000.0,3000000.0,p36.32,#949494, g1
2, 5300000.0,7100000.0,1800000.0,p36.31,#FFFFFF,  g1
3, 7100000.0,9100000.0,2000000.0,p36.23,#949494, g1
4, 9100000.0,12500000.0,3400000.0,p36.22,#FFFFFF, g1

I want to create an horizontal stacked barchar with the following output:

| - indx[1] [len=bar_len] | - indx[2] [len=bar_len] | - indx[3]
[len=bar_len] | - indx[4] [len=bar_len]

I tried doing this the following way:

import plotly.express as px
import pandas as pd

input_path = r"example.csv"
df = pd.read_csv(input_path)
df.set_index('start')
fig = px.bar(
    df, x='bar_len', y='gr', color="DS_COLOR", orientation='h',
)

fig.update_layout(barmode='stack', xaxis={'categoryorder':'category ascending'})

The problem is that the values plotted on the barchar are not sorted by start column, which is what I am trying to do. Therefore, my question is: is there any way to plot a stacked bachar that plots the length of each of the elements based on one of the columns (bar_len) and sorts these plotted elements based on another column (start)?

UPDATE: I have seen that the problem raises when including the color label. This label resorts the barchart based on the color instead of preserving the original order based on index column. Is there any way to avoid this?

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

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

发布评论

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

评论(1

佼人 2025-02-04 08:51:23

您可以使用plotly graph_objects构建它。下面的代码要完成必要的。 注意:在数据框架中,我将颜色更改为 hex代码,它是红色的#ff0000,blue的#0000ff。我仅使用 bar_len color gr 列。通过此答案
df 看起来

    start   end bar_len name    color   gr
0   2300000.0   5300000.0   3000000.0   p36.32  #FF0000 g1
1   5300000.0   7100000.0   1800000.0   p36.31  #0000FF g1
2   7100000.0   9100000.0   2000000.0   p36.23  #FF0000 g1
3   9100000.0   12500000.0  3400000.0   p36.22  #0000FF g1

代码在这里:

import pandas as pd
import plotly.graph_objects as go

input_path = r"example.csv"
df = pd.read_csv(input_path)

data = []
for i in range(len(df)):
    data.append(go.Bar(x=[df['bar_len'][i]], y=[df['gr'][i]], marker=dict(color=df['color'][i]), orientation = 'h'))
layout = dict(barmode='stack', yaxis={'title': 'gr'}, xaxis={'title': 'Length'})
fig = go.Figure(data=data, layout=layout)
fig.update_layout(showlegend=False, autosize=False, width=800, height=300)
fig.show()

输出图

”

注意:如果X-轴可以表示为时间表,并且您可以将X值作为DateTime获得您还可以查看 plotly.express.timeline 图表,该图表给出了图形的图表。示例在这里 - 检查第一个图表...

You can build it using plotly graph_objects. Code below to do the needful. Note: In the dataframe, I changed the color to HEX CODE which is #FF0000 for RED and #0000FF for BLUE. I have used only bar_len, color and gr columns. Adopted from this answer.
df looks like this

    start   end bar_len name    color   gr
0   2300000.0   5300000.0   3000000.0   p36.32  #FF0000 g1
1   5300000.0   7100000.0   1800000.0   p36.31  #0000FF g1
2   7100000.0   9100000.0   2000000.0   p36.23  #FF0000 g1
3   9100000.0   12500000.0  3400000.0   p36.22  #0000FF g1

The code is here:

import pandas as pd
import plotly.graph_objects as go

input_path = r"example.csv"
df = pd.read_csv(input_path)

data = []
for i in range(len(df)):
    data.append(go.Bar(x=[df['bar_len'][i]], y=[df['gr'][i]], marker=dict(color=df['color'][i]), orientation = 'h'))
layout = dict(barmode='stack', yaxis={'title': 'gr'}, xaxis={'title': 'Length'})
fig = go.Figure(data=data, layout=layout)
fig.update_layout(showlegend=False, autosize=False, width=800, height=300)
fig.show()

OUTPUT GRAPH

Graph

Note: If the x-axis can be expressed as a timeline and you are able to get the x values as datetime, would suggest you also check out plotly.express.timeline charts which gives gantt chart form of graphs. Sample here - Check the first chart...

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