首页 > 解决方案 > Plotly:订购甘特图以在左上角而不是左下角显示最早的项目

问题描述

我正在尝试使用 plotly 制作一个简单的甘特图,但由于某种原因,最早的项目显示在左下角,而不是应该的左上角。

有没有办法改变这个?该示例来自以下文档(已修复,因此可以运行):

import plotly.figure_factory as ff

df = [dict(Task="Job A", Start='2009-01-01',
       Finish='2009-02-28', Complete=10),
      dict(Task="Job B", Start='2009-03-05',
       Finish='2009-04-15', Complete=60),
      dict(Task="Job C", Start='2009-02-20',
       Finish='2009-05-30', Complete=95)]

fig = ff.create_gantt(df, colors='Blues', index_col='Complete',
                   show_colorbar=True, bar_width=0.5,
                   showgrid_x=True, showgrid_y=True)
fig.show()

代码输出

我们将忽略 showgrid 似乎不起作用。这是因为默认颜色是白底白字。

标签: pythonplotlygantt-chart

解决方案


import plotly.figure_factory as ff

df = [dict(Task='Job A', Start='2009-01-01', Finish='2009-02-28', Complete=10),
      dict(Task='Job B', Start='2009-03-05', Finish='2009-04-15', Complete=60),
      dict(Task='Job C', Start='2009-02-20', Finish='2009-05-30', Complete=95)]

fig = ff.create_gantt(df, colors='Blues', index_col='Complete', show_colorbar=True,
                      bar_width=0.5, showgrid_x=True, showgrid_y=True)

fig.update_layout(plot_bgcolor='white', # change the plot background color
                  yaxis=dict(autorange='reversed', # reverse the order of the y-axis tick labels
                             linecolor='gray',  # change the color of the y-axis line
                             gridcolor='gray'), # change the color of the y-axis grid lines
                  xaxis=dict(linecolor='gray',  # change the color of the x-axis line
                             gridcolor='gray')) # change the color of the x-axis grid lines

fig.show()

在此处输入图像描述


推荐阅读