Sorted categories (Pandas `Categorical` series) aren't sorted in chart
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 18.8k
- Fork
- 2.8k
- Merge medio
- 16h 26m
- PR unite (30g)
- 21
Descrizione
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?
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Riproduci il problema con il pandas DataFrame fornito e l'entry point px.scatter(df, x="Rating", y="Cost"), quindi traccia come vengono gestiti i valori categorici degli assi. Conferma che le categorie ordinate determinano l'ordine dell'asse e aggiungi una copertura di regressione; il lavoro è completo quando il grafico segue Low, Medium, High anziché l'ordine dei dati.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- pandas, python
- Ambito
- data-visualization
- Tipo di issue
- Bug
- Difficoltà
- 3/5
- Tempo stimato
- 1-2 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 42/100