0

With a simple dataFrame that look like this

df

The following code

fig = px.timeline(df, 
              x_start="Target Date Start", 
              x_end="Target Date End", 
              y="Initiative", 
              color="Status Bin", 
              facet_row="Project Key",
              template="plotly_white") 

Generates a graph that does not adjust based on facet row. I would expect only initiatives associated with a given project key to show in the y marks, but instead all initiatives are shown: enter image description here

Note that it shows only the relevant bars but it shows all y marks as opposed to filter for relevant initiatives and also it keeps the size the same across facet_rows whereas I would expect their size to be proportional to the number of initiatives in their resp. group

EDIT: r-beginners suggested fix

enter image description here

0

1 Answer 1

1

If your goal is to optimize the y-axis for each individual subplot, rather than to make the y-axis values common to all subplots, then a graph object can handle this, but it will break the graph visualized in the well-organized x and y axes.

import plotly.express as px
import plotly.graph_objects as go
from plotly.subplots import make_subplots

row_cnt = len(df['Project Key'].unique())
fig = make_subplots(rows=row_cnt, cols=1)

for idx,k in enumerate(df['Project Key'].unique()):
    dff = df[df['Project Key'] == k]
    print(dff)
    tl = px.timeline(dff,
                      x_start="Target Date Start",
                      x_end="Target Date End",
                      y="Initiative",
                      color="Status Bin")
    for t in range(len(tl.data)):
        fig.add_trace(tl.data[t], row=idx+1, col=1)
        fig.update_traces(width=0.1)
        fig.update_traces(legendgroup=k, legendgrouptitle_text=f'Project Key:{k}', row=idx+1, col=1)
        fig.update_xaxes(type='date', range=[df['Target Date Start'].min(),df['Target Date End'].max()]) 
    
fig.update_layout(height=1200, width=1000, template="plotly_white")

fig.show()
7
  • Thanks @r-beginners! Definitely heading in the right direction. First reaction is wow, you have completely got rid of this facet_row param, which I thought was meant for this. Is there a way to keep x-axis consistent across subplots? And also bar sizes? And finally keep legend neat with similar entries grouped together?
    – Mth Clv
    Commented Jun 17 at 15:47
  • See EDIT in my post for the legend issue I was talking about. Also, you'll see bar size problem. Just realized the first plot should have 22 entries, and indeed 22 bars are shown but the y labels are not adjusting. Can this size dynamically?
    – Mth Clv
    Commented Jun 17 at 15:51
  • To illustrate from where we can, unification of the x-axis is possible by specifying a minimum start date and maximum end date for each subplot. The width of the bars can be specified, but they will not be the same width since they will be optimized by the elements of the y-axis. Finally, the legend, of course, is not the same as in express, since the graph is extracted by key and looped through the process. I set the legend group by key and created the group title with the key name. Commented Jun 18 at 1:36
  • Thank you! Last one before I accept your answer. The first plot should have 22 entries, and indeed 22 bars are shown but the y labels are not adjusting and only 9 initiatives are shown in y labels - which mean they are also misaligned. How do we resolve this?
    – Mth Clv
    Commented Jun 18 at 8:20
  • 1
    As I understand it, the Facet graphs provided by plotly.express have the same x-axis y-axis, respectively. s functional introduction can be found here Commented Jun 18 at 9:27

Not the answer you're looking for? Browse other questions tagged or ask your own question.