Sergei Zobov
Sergei Zobov

Reputation: 409

Y-axis doesn't fit the number of timelines

I have a plot made with px.timeline with a lot of input data and I see that numbers in rows on Y-axis are different from the number of bars. It seems like plotly didn't scale Y-axis to fit a number of time bars, but I didn't find any parameters to change it.

This is an example of how it looks:

enter image description here

But if I will zoom in to the smaller region with fewer amounts of bars the Y-axis starts to fit:

enter image description here

Are there any ways to make the plot kinda "scrollable" or re-calculate font size to align the Y-axis with a number of timelines? Thanks.

Upvotes: 0

Views: 592

Answers (1)

Rob Raymond
Rob Raymond

Reputation: 31146

  • from image it appears that yaxis is categorical. Have simulated data to replicate
  • you can effectively control yaxis through range. Have built a drop down menu that will allow user to select section of yaxis
import plotly.express as px
import pandas as pd

df = pd.DataFrame({"Task": [f"{a}_{b}" for a in range(8) for b in range(15)]}).assign(
    Start=lambda d: pd.Series(pd.date_range("1-jan-2010", periods=len(d) * 10))
    .sample(len(d))
    .values,
    Finish=lambda d: d["Start"] + pd.Timedelta(days=25),
)

fig = px.timeline(df, x_start="Start", x_end="Finish", y="Task")

BARS = 15
fig.update_layout(
    updatemenus=[
        {
            "buttons": [
                {
                    "label": f"{fig.data[0].y[s]} to {fig.data[0].y[min(s+BARS, len(fig.data[0].y)-1)]}",
                    "method": "relayout",
                    "args": [{"yaxis": {"range": [s, s + BARS]}}],
                }
                for s in range(0, len(fig.data[0].y), BARS)
            ],
            "active": -1,
        }
    ]
)

Upvotes: 1

Related Questions