plotly / plotly/plotly.py

add_vline/hline annotation text location fixed to first shape if using a pre-instantiated dict for the "annotation" input parameter

Đang mở
#3,547 1 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

bug P3
Ngôn ngữ chính
Python
Star
18.8k
Fork
2.8k
Merge trung bình
16 giờ 26 phút
Pull request đã merge (30 ngày)
21

Mô tả

Hi

When I instantiate a dict once with some constant formatting that I want to pass to the "annotation" argument for several calls of .add_vline() (or .add_hline()), all annotations for shapes created with this dict are placed with the shape created by the initial .add_vline() call.

e.g.:

import plotly.graph_objects as go

fig = go.Figure()

y_1 = [3000, 3500, 4000, 3500, 3000, 3500]
x_1 = list(range(len(y_1)))
fig.add_trace(go.Scatter(x=x_1, y=y_1))


annotation = dict(font_color="#000000")

fig.add_vline(x=2, annotation_text="vline 1", annotation=annotation)

print(f"annotation after first .add_vline():\n{annotation}")

fig.add_vline(x=3, annotation_text="vline 2", annotation=annotation)

print(f"annotation after second .add_vline():\n{annotation}")


fig.write_html("graph.html")

produces graph and output (similar result if .add_hline() used instead) - expected graph can be seen at the end of this post:
image

annotation after first .add_vline():
{'font_color': '#000000', 'text': 'vline 1', 'xanchor': 'left', 'yanchor': 'top', 'x': 2, 'y': 1, 'showarrow': False}
annotation after second .add_vline():
{'font_color': '#000000', 'text': 'vline 2', 'xanchor': 'left', 'yanchor': 'top', 'x': 2, 'y': 1, 'showarrow': False}

Is this expected behaviour? As the x coordinate is explicitly given in the method I would expect that the annotation defined would be placed at that x, as happens with the different annotation_text values.

As we can see from the output, it is because the original annotation dictionary instance is being mutated in the process of adding the annotation. Then if an x value is present in the annotation dictionary, it is not overwritten, but the text value is if a new annotation_text value is passed.

Looking at the package code, I can achieve the behaviour I expected by implementing an else here:
https://github.com/plotly/plotly.py/blob/cfad7862594b35965c0e000813bd7805e8494a5b/packages/python/plotly/plotly/shapeannotation.py#L203-L204
Suggestion:

    if annotation is None:
        annotation = dict()
    else:
        annotation = annotation.copy()

I have made this change locally and run fine and as I expect.

If the current behaviour is not the expected behaviour, and if the suggested change above is acceptable, I would be glad to implement and test it appropriately then submit a pull request.

Cheers!


Graph I expect:
image

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu trong plotly/shapeannotation.py tại các dòng 203-204 và tái hiện vấn đề bằng các lần gọi add_vline() lặp lại với một dictionary annotation duy nhất, sau đó so sánh với add_hline(). Được xem là hoàn tất khi dictionary được cung cấp không bị thay đổi và annotation của mỗi shape sử dụng tọa độ riêng của nó; thêm hoặc cập nhật các test cho cả hai method khi phù hợp.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
python
Lĩnh vực
data-visualization
Loại issue
Lỗi
Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Đặc tả rõ ràng
Mức phù hợp với người mới
45/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.