8000 Alphabetically sort the legend in px.pie / vm.Graph while keeping slice order by value · Issue #1146 · mckinsey/vizro · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
Alphabetically sort the legend in px.pie / vm.Graph while keeping slice order by value #1146
Closed
@fpeucelle

Description

@fpeucelle

Have you already looked into this topic?

  • I've reviewed the Vizro documentation for any relevant information
  • I've searched through existing issues for similar questions
  • I've already searched online (e.g., Dash documentation) but couldn’t find anything helpful

Question

Hi Vizro team 👋,

First of all, thanks for the great work—Vizro is powering our judicial analytics dashboards and it’s been a pleasure to use. I have a question (and maybe a feature request) regarding pie‑chart legends.

When users switch filters or jump between tabs, the slice order (by value) can change from one state to another. Because the legend always mirrors that slice order, its entries shuffle around and users lose their visual bearings. An alphabetically fixed legend would reduce that cognitive load.

What I’ve tried

  1. category_orders controls both slice order and legend order, so decoupling them isn’t possible there.
  2. layout.legend.traceorder only accepts "normal" | "reversed" | "grouped"—no custom alphabetical order.
  3. A workaround with invisible traces (go.Scatter(x=[None], y=[None])) creates a fake legend, but it breaks the native interactivity (click‑to‑hide) and bloats the figure for large category counts.

Question / Feature Request
Is there any built‑in way in Vizro or Plotly to break the coupling and:

keep the slice order driven by values, but supply an independent alphabetical order for the legend (so it stays stable across filter and tab changes)?

If not, would you consider adding something like legend_category_orders (analogous to category_orders but applied only to the legend)?

Thanks a lot for your time and for maintaining Vizro!
Greetings from Chubut, Argentina 🇦🇷
Francisco

Code/Examples

import pandas as pd
import vizro.plotly.express as px
import vizro.models as vm
from vizro import Vizro

df = pd.DataFrame({
    "descItem": ["Rawson", "Comodoro", "Trelew", "Esquel"],
    "Valor":    [20, 12, 7, 15]
})

pie_df   = df.sort_values("Valor", ascending=False)   # slice order by value
legend_alphabetic = sorted(df["descItem"])            # desired legend order

color_map = {
    "Comodoro": "#1f77b4",
    "Esquel":   "#ff7f0e",
    "Rawson":   "#2ca02c",
    "Trelew":   "#d62728",
}

fig = px.pie(
    pie_df,
    names="descItem",
    values="Valor",
    category_orders={"descItem": pie_df["descItem"].tolist()},
    color="descItem",
    color_discrete_map=color_map,
)
fig.update_traces(sort=False)  # keep slice order as supplied

page = vm.Page(components=[vm.Graph(figure=fig)])
Vizro().build(vm.Dashboard(pages=[page])).run()

Which package?

vizro

Code of Conduct

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

    0