plotly / plotly/plotly.py

Allow ticktext for minor ticks

Open
#4,911 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature P3
Dominant language
Python
Stars
18.8k
Forks
2.8k
Avg merge
16h 26m
Merged PRs (30d)
21

Description

Here is the baseline plot :

import plotly.express as px
import pandas as pd
from datetime import timedelta

# Example data
df = pd.DataFrame({
    "Task": ["Task A", "Task B", "Task C"],
    "Start": ["2023-01-01 08:00:00", "2023-01-02 09:30:00", "2023-01-04 11:00:00"],
    "Finish": ["2023-01-01 15:00:00", "2023-01-02 20:00:00", "2023-01-04 19:00:00"],
})

# Create the timeline figure
fig = px.timeline(
    df,
    x_start="Start",
    x_end="Finish",
    y="Task",
    title="Timeline with major and minor ticks on x axis",
)
df['Start'] = pd.to_datetime(df['Start'])
df['Finish'] = pd.to_datetime(df['Finish'])

# Define tick positions and labels
horizon_start = df['Start'].min().replace(hour=0, minute=0)
horizon_end = df['Finish'].max().replace(hour=0, minute=0) + timedelta(days=1)
minor_ticks = pd.date_range(horizon_start, horizon_end, freq="6h")

fig.update_xaxes(
    showgrid=True,
    gridcolor="lightgray",
    gridwidth=2,
    dtick="D1",
    tickformat="%e %b",
    tickangle=-45,
    minor={
        "ticklen": 3,
        "tickcolor": "lightgray",
        "tickmode": "array",
        "showgrid": True,
        "gridwidth": 0.5,
        "tickvals": minor_ticks,
    },
)

# Show the figure
fig.show()

Image

I would like to have the possibility of displaying ticktext for minor ticks + ticktextcolor + ticktextfontsize, with something like :

minor_text = [t.strftime('%Hh').lstrip('0') if t.hour != 0 else "0h" for t in minor_ticks]

fig.update_xaxes(
    (...)
    minor={
        "ticklen": 3,
        "tickcolor": "lightgray",
        "tickmode": "array",
        "showgrid": True,
        "gridwidth": 0.5,
        "tickvals": minor_ticks,
        "ticktext": minor_text,
        "ticktextcolor": 'lightgray',
        "ticktextfontsize": 5,
    },
)

Ideally, for the ticks that are both in major and minor ticks, only the major tick text would be displayed.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start from the fig.update_xaxes entry point and the minor-axis configuration shown in the issue. Trace how minor tick properties are represented and passed through the Python graph-object API. Done means minor ticks accept ticktext, ticktextcolor, and ticktextfontsize, while major tick labels take precedence where positions overlap.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
data-visualization
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.