plotly / plotly/plotly.py

px.imshow requires int for facet_col while px.scatter can be str

Đang mở
#3,296 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ả

px.imshow produces an error if the dimension of a facet plot has strings as their labels instead of integers. I ran across this with xarray, however, I believe it is present regardless of the input data structure. The other px graphs, e.g. px.scatter, do allow strings in the facet dimension, so this seems like a bug with imshow. https://plotly.com/python/facet-plots/ has many plotting examples with strings as the facet_col, e.g. "sex=Female" and "sex=Male" from fig = px.scatter(df, x="total_bill", y="tip", color="smoker", facet_col="sex") and many others from px.histogram, px.choropleth, etc.

Versions in use:
python=3.9.1
plotly=4.14.3

Here is an example of the error.

>>> import numpy as np
>>> import xarray as xr
>>> import plotly.express as px
>>> row = xr.Variable('img_row', range(4))
>>> col = xr.Variable('img_col', range(4))
>>> fd = xr.Variable('facet_dim', 'QA QB'.split())
>>> a = [xr.DataArray(np.random.rand(4,4), coords=[row, col]) for i in range(2)]
>>> da_str = xr.concat(a, "facet_dim")
>>> da_var = xr.concat(a, fd)
>>> da_str
<xarray.DataArray (facet_dim: 2, img_row: 4, img_col: 4)>
array([[[0.17020614, 0.23346368, 0.55878844, 0.58773312],
        [0.80092718, 0.25899341, 0.12681188, 0.77129175],
        [0.85068779, 0.48145364, 0.72109667, 0.51325248],
        [0.2659319 , 0.62613397, 0.09588715, 0.59035821]],

       [[0.32991787, 0.59718269, 0.60165123, 0.63523444],
        [0.11699783, 0.19545503, 0.22478829, 0.56217593],
        [0.59725   , 0.34207063, 0.29841437, 0.30079022],
        [0.21422747, 0.74626584, 0.86025186, 0.61071694]]])
Coordinates:
  * img_row  (img_row) int32 0 1 2 3
  * img_col  (img_col) int32 0 1 2 3
Dimensions without coordinates: facet_dim
>>> da_var
<xarray.DataArray (facet_dim: 2, img_row: 4, img_col: 4)>
array([[[0.17020614, 0.23346368, 0.55878844, 0.58773312],
        [0.80092718, 0.25899341, 0.12681188, 0.77129175],
        [0.85068779, 0.48145364, 0.72109667, 0.51325248],
        [0.2659319 , 0.62613397, 0.09588715, 0.59035821]],

       [[0.32991787, 0.59718269, 0.60165123, 0.63523444],
        [0.11699783, 0.19545503, 0.22478829, 0.56217593],
        [0.59725   , 0.34207063, 0.29841437, 0.30079022],
        [0.21422747, 0.74626584, 0.86025186, 0.61071694]]])
Coordinates:
  * img_row    (img_row) int32 0 1 2 3
  * img_col    (img_col) int32 0 1 2 3
  * facet_dim  (facet_dim) <U2 'QA' 'QB'
>>> fig = px.imshow(da_str, facet_col='facet_dim', aspect='equal')
>>> [d.__class__.__name__ for d in fig.data]
['Heatmap', 'Heatmap']
>>> fig = px.imshow(da_var, facet_col='facet_dim', aspect='equal')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\<NAME>\Miniconda3\envs\jupyter\lib\site-packages\plotly\express\_imshow.py", line 525, in imshow
    col_labels = ["%s=%d" % (slice_label, i) for i in facet_slices]
  File "C:\Users\<NAME>\Miniconda3\envs\jupyter\lib\site-packages\plotly\express\_imshow.py", line 525, in <listcomp>
    col_labels = ["%s=%d" % (slice_label, i) for i in facet_slices]
TypeError: %d format: a number is required, not numpy.str_

I think the culprit is the different string conversions in play. Can the px.imshow col_labels use a more forgiving conversion?
From File "C:\Users\<NAME>\Miniconda3\envs\jupyter\lib\site-packages\plotly\express\_imshow.py", line 525, in imshow
col_labels = ["%s=%d" % (slice_label, i) for i in facet_slices]
From File "C:\Users\<NAME>\Miniconda3\envs\jupyter\lib\site-packages\plotly\express\_core.py", line 1885, in make_figure
col_labels = [prefix + str(s) for s in sorted_group_values[m.grouper]]

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/express/_imshow.py tại dòng được báo cáo 525, nơi các nhãn facet được định dạng, và so sánh với cách xử lý nhãn được thể hiện trong plotly/express/_core.py. Tái hiện ví dụ px.imshow bằng cách sử dụng facet_dim có giá trị chuỗi, sau đó xác minh rằng các nhãn facet dạng chuỗi không còn gây ra TypeError và vẫn tạo ra các facet heatmap như mong đợi.

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
35/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.