Sorted categories (Pandas `Categorical` series) aren't sorted in chart
Chưa có ai nhận issue này.
- 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
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- 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.
- Fork repository và làm thay đổi trên một nhánh.
- 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