我有一个看起来像这样的数据框:
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?
发布评论
评论(1)
您可以使用plotly graph_objects构建它。下面的代码要完成必要的。 注意:在数据框架中,我将颜色更改为
hex代码
,它是红色的#ff0000,blue的#0000ff。我仅使用bar_len
,color
和gr
列。通过此答案。df
看起来代码在这里:
输出图
注意:如果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 onlybar_len
,color
andgr
columns. Adopted from this answer.df
looks like thisThe code is here:
OUTPUT 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...