散景四边形图中的重排条

问题描述 投票:0回答:1

我正在尝试使用Bokeh Quad字形创建甘特图,并且由于某些原因,条形按相反的时间顺序添加。

Here is an image of the bokeh output that I'm getting.

我正在使用熊猫数据框创建ColumnDataSource对象,无论我如何对数据框进行排序或重新索引,它似乎都对Quad的布局没有影响。我希望条形图出现在按开始日期和升序排列的活动中,就像正常的甘特图一样。

是否有已知的方法可以根据源列重新组织四边形中的条形?

供参考,这是我用来生成上面链接的图像的代码:

from bokeh.plotting import figure, show, output_file
from bokeh.models import ColumnDataSource, Range1d
from bokeh.models.tools import HoverTool
from datetime import datetime
import pandas as pd

output_file('gantt_chart.html') 

data_dict = {'activityRef': ['c.001.50', 'c.002.51', 'c.073.51', 'c.074.51', 'c.003.52', 'c.004.53', 'c.079.53', 'c.080.53', 'c.005.54', 'c.084.54', 'c.006.60', 'c.088.61', 'c.091.61', 'c.092.61', 'c.007.61', 'c.093.61', 'c.094.61', 'c.095.61', 'c.096.62', 'm.003.62', 'c.008.62'], 'activityPrefix': ['Demolition', 'Grading', 'Structural Excavation, Backfill', 'MechElec Excavation, Backfill', 'Utilities', 'Civil Paving', 'Aggregates', 'Material Processing, Plants', 'Temporary Work', 'Scaffolding', 'Deep Foundation', 'Concrete Paving', 'Concrete Foundations', 'MechElec Concrete', 'Precast Concrete', 'Concrete Thermal Control', 'Rebar Placement', 'Anchor Bolts, Embedments', 'Structural Welding', 'Module Assembly', 'Erect Steel'], 'disciplineRef': ['Civil', 'Civil', 'Civil', 'Civil', 'Civil', 'Civil', 'Civil', 'Civil', 'Temporary Work', 'Temporary Work', 'Civil', 'Concrete', 'Concrete', 'Concrete', 'Concrete', 'Concrete', 'Concrete', 'Concrete', 'Structural', 'Structural', 'Structural'], 'duration': [10, 12, 18, 40, 25, 15, 10, 12, 18, 40, 25, 15, 10, 12, 18, 40, 25, 15, 10, 12, 18]}

df = pd.DataFrame(data_dict)

df['start'] = '6/16/2020'
df['start'] = pd.to_datetime(df['start'])
df['start'] = [d + pd.Timedelta(days=i) for i, d in enumerate(df.start)]
df['end'] = df.apply(lambda row: row.start + pd.Timedelta(days=row.duration), axis=1)
df['color'] = 'blue'
df['activity'] = df.activityPrefix + ' - ' + df.activityRef
df['bottom'] = df.index+0.8
df['top'] = df.index+1.2


fig=figure(
    title='Project Schedule',
    x_axis_type='datetime',
    x_axis_location="above",
    width=1900,
    height=(df.shape[0] * 12),
    y_range=df.activity.tolist(),
    x_range=Range1d(df.start.min(), df.end.max()), 
)

hover=HoverTool(tooltips="Task: @activity<br>\
Start: @start<br>\
End: @end")
fig.add_tools(hover)

cds=ColumnDataSource(df)

fig.quad(left='end', right='start', bottom='bottom', top='top', source=cds, color="color")

show(fig)

提前感谢!

python pandas bokeh
1个回答
0
投票

我在上面的问题中找到了解决方案:仅使用hbar字形即可提供正确的替代实现:

fig.hbar(y='activity', height=0.5, left='end', right='start', source=cds, color="color")
© www.soinside.com 2019 - 2024. All rights reserved.