plotly / plotly/plotly.py

Sorted categories (Pandas `Categorical` series) aren't sorted in chart

Đang mở
#3,802 1 bình luận 1 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ả

I am plotting a chart with a pd.Categorical series on the x-axis. The category has a sort defined.

Expected behaviour: the axis is sorted as per the underlying category definition
Actual behaviour: the axis is sorted by the order of the data

df = pd.DataFrame(
    dict(
        Cost=[10, 20, 15, 2, 9],
        Rating=pd.Categorical(
            values=["High", "Low", "Medium", "Low", "Medium"],
            categories=["Low", "Medium", "High"],
            ordered=True,
        ),
    )
)

fig = px.scatter(df, x="Rating", y="Cost")

This fix would be fairly simple, I think, something like this, where series is whatever's being rendered to the xaxis (or y, or legend, etc)

if isinstance(series.dtype, pd.CategoricalDtype) and series.cat.ordered:
    fig.update_xaxes(
        categoryorder="array",
        categoryarray=series.cat.categories,
    )

Plotly version: 5.9.0.

I've noticed a number of bugs relating to pd.Categorical and pd.PeriodIndex, are there plans for better Pandas support, or is that not an area of focus?

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

Tái hiện vấn đề với pandas DataFrame được cung cấp và entry point px.scatter(df, x="Rating", y="Cost"), sau đó theo dõi cách các giá trị phân loại của trục được xử lý. Xác nhận rằng các category có thứ tự quyết định thứ tự của trục và bổ sung regression coverage; hoàn tất nghĩa là biểu đồ tuân theo Low, Medium, High thay vì thứ tự dữ liệu.

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

Đánh giá

Công nghệ
pandas, python
Lĩnh vực
data-visualization
Loại issue
Lỗi
Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
42/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.