Skip to content

Bars doesn’t respect dict ordering #6696

@G-Guillard

Description

@G-Guillard

ALL software version info

Software Version Info
holoviews 1.21.0
bokeh 3.6.3, 3.8.0
python 3.11.11, 3.12.10

Description of expected behavior and the observed behavior

Since Python 3.7, dictionaries respect insertion ordering. As a consequence, when applying hv.Bars on a dict, one expects the bar plot to respect the dict ordering as well. However hv.Bars relies on the keys alphabetical or numerical ordering instead. Note that it behaves the same way with an explicit OrderedDict.

In addition, the documentation on how to use redim is somehow confusing.

Complete, minimal, self-contained example code that reproduces the issue

import holoviews as hv
from collections import Counter, OrderedDict

a = {'B': 9, 'A': 4, 'C': 1}
d = OrderedDict(a)

## don’t work -> ABC
hv.Bars(a)
# hv.Bars(d)

## work -> BAC
# hv.Bars(a, hv.Dimension("foobar")).redim.values(foobar=list(a.keys()))
# hv.Bars(a, hv.Dimension("foobar", values=list(a.keys())))
# hv.Bars(a).sort(by="y", reverse=True)
# hv.Bars(list(d.items()))

## some working alternatives
# b = sorted(a.items(), key=lambda x: x[1], reverse=True)
# hv.Bars(b)  # works -> BAC
# c = Counter(a).most_common()
# hv.Bars(c)  # works -> BAC

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions