plotly / plotly/plotly.py

Cannot increase distance between shape and its label outside of the object

未關閉
#4,971 1 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

feature P3
主要語言
Python
星號
18.8k
分支
2.8k
平均合併
16 小時 26 分鐘
30 天內合併 PR
21

描述

Challenge

I would like to draw a shape using the label argument to position the label outside of the shape. Unfortunately, the label always touches the shape and padding only works in one direction: towards the center of the shape.

Here is an example:

import plotly.graph_objects as go

fig = go.Figure()

for i in range(0, 4):
    fig.add_shape(
        go.layout.Shape(
            type="rect",
            x0=10,
            y0=10*i + 3,
            x1=15,
            y1=10*i + 10,
            label={
                "text": f"Padding: {i*10}px",
                "textposition": "middle right",
                "xanchor": "left",
                "yanchor": "middle",
                "padding": i*10,
            },
        )
    )

fig.update_layout(xaxis_range=[0, 20], yaxis_range=[0, 60])

fig.show()

Despite seting xanchor="left" and textposition="middle right", the label barely touches the shape (for padding=0) and with increasing padding the label always moves towards the center.

Image

Setting padding to a negative value seems not to be allowed:

ValueError: 
    Invalid value of type 'builtins.int' received for the 'padding' property of layout.shape.label
        Received value: -10

    The 'padding' property is a number and may be specified as:
      - An int or float in the interval [0, inf]

Expected Behavior

Either infer from anchor and textposition, if a label should be outside of the shape, or allow negative values for padding so that I can increase the distance between the rectangle and the label.

I could not find any other info regarding this observation and I would like to avoid using an annotation as a workaround.

Am I overlooking something or is this a bug?

System Info

python: v3.10.11
plotly: v5.24.1

Working in a VSCode (v1.96.2) notebook and tested multiple different renderers with identical outcome: "vscode", "notebook", "browser".
Same result with write_image and pdf and png.

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

研究方向

首先,使用不同的 padding 值和 renderer 執行提供的 go.Figure/add_shape 範例。追蹤 shape label 如何處理 textposition、xanchor 和 padding;當 label 無需 annotation workaround 即可向 shape 外側留出間距,且針對所示案例具備回歸測試涵蓋時,該 issue 即完成。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
python
領域
data-visualization
Issue 類型
缺陷
難度
4/5
預估耗時
3-5 天
活躍度
活躍
描述清晰度
基本清楚
新手友好度
48/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。