plotly / plotly/plotly.py

Ticklabels can not be middle aligned when using a weekly period

未关闭
#5,293 0 条评论 1 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

bug P3
主要语言
Python
星标
18.8k
派生
2.8k
平均合并
16 小时 26 分钟
30 天内合并 PR
21

描述

Plotly version: 6.2.0

Minimal Example
import plotly.graph_objects as go
import pandas as pd

data = [
    {"date": "2025-07-20", "total_sales": 23.34},
    {"date": "2025-07-27", "total_sales": 45.21},
    {"date": "2025-08-03", "total_sales": 12.98},
    {"date": "2025-08-10", "total_sales": 41.32},
]

df = pd.DataFrame.from_records(data)

def make_figure(df, period, step, title):
    fig = go.Figure()
    fig.add_trace(go.Bar(
        x=df['date'],
        y=df['total_sales'],
        xperiod=period,
        xperiod0=df['date'].min(),
        xperiodalignment="middle"
    ))
    fig.update_layout(
        xaxis={
            'ticklabelmode': 'period',
            'tickmode': 'linear',
            'dtick': period,
            'tick0': df['date'].min(),
            'ticklabelstep': step
        },
        title={
            'text': title
        })
    fig.show(renderer="browser")

MS_PER_DAY = 24 * 60 * 60 * 1000
make_figure(df, MS_PER_DAY, 7, "Period = 1 Day")
make_figure(df, MS_PER_DAY * 7, 1, "Period = 7 Days")

When plotting aggregated timeseries data in period mode, the ticklabels are aligned to the middle of the bar when the period is 1 day or even 1 month, but if I try to use a period of 1 week the ticklabels are aligned to the left side of the column.

Image Image

I cannot use ticklabelshift to move the labels towards the center since ticklabelshift requires the measurement in pixels and what I'm building is repsonsive, so the width of the chart in pixels is variable

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

首先运行报告中的最小 Python 示例,并比较日、月和周周期的情况。跟踪周周期的周期模式刻度标签对齐行为,并确定相关的 Plotly 实现和回归测试位置。完成的标准是:周标签能够自适应地居中,且不进行基于像素的偏移,同时其他周期情况保持正确。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
data-visualization
Issue 类型
缺陷
难度
3/5
预计耗时
1-2 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
48/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。