Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

[BUG]: Single-color requests raise ZeroDivisionError in palette helpers

未关闭 适合新手
#5,805 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

@MokiMeow 已经在做这个了。

开始于 2026年10月10日。

  • #5806 来自 @MokiMeow —— 未关闭

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
82/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
python

调研方向

问题出在 plotly.colors 辅助函数 sample_colorscale 和 n_colors 中,它们都除以请求的数量减一。首先运行 issue 中的复现脚本,确认数量为 1 时会抛出 ZeroDivisionError。当请求单个颜色时返回区间起点(low / lowcolor)处的一个颜色,与多颜色情况的第一个采样结果一致,并且零颜色的情况仍然返回空列表,即为完成。

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

描述

Description

sample_colorscale and n_colors raise ZeroDivisionError when asked for one color. This breaks palettes sized to a dataset's group count when there is exactly one group. Both helpers divide by the requested count minus one.

Expected: one color from the start of the interval (low / lowcolor), consistent with their existing first sample.

Screenshots/Video

The API calls fail before a figure can be created; the output is shown below.

Steps to reproduce
from plotly.colors import n_colors, sample_colorscale

for label, call in [
    ("sample_colorscale", lambda: sample_colorscale("Viridis", 1)),
    ("n_colors", lambda: n_colors((0, 0, 0), (1, 1, 1), 1)),
]:
    try:
        print(label, call())
    except ZeroDivisionError as error:
        print(label, type(error).__name__, str(error))

Actual:

sample_colorscale ZeroDivisionError division by zero
n_colors ZeroDivisionError float division by zero

Expected:

sample_colorscale ['rgb(68, 1, 84)']
n_colors [(0.0, 0.0, 0.0)]
Notes

Reproduced with Plotly 7.1.0, current main 3f8a650930feadfdb651cd869ffe3e85fd093885, Python 3.12.10, Windows amd64. Zero-color requests already return empty lists.

主要语言
Python
星标
18.8k
派生
2.9k
平均合并
3 天 7 小时
30 天内合并 PR
20

环境准备

从这里开始

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

plotly/plotly.py 的其他 Issue

查看 plotly/plotly.py 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

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