Skip to content

Accept NumPy axis labels in annotated heatmaps - #5695

Open
nyxst4ck wants to merge 1 commit into
plotly:mainfrom
nyxst4ck:fix/annotated-heatmap-numpy-xy
Open

Accept NumPy axis labels in annotated heatmaps#5695
nyxst4ck wants to merge 1 commit into
plotly:mainfrom
nyxst4ck:fix/annotated-heatmap-numpy-xy

Conversation

@nyxst4ck

Copy link
Copy Markdown

Description

figure_factory.create_annotated_heatmap() documents NumPy arrays for z, but passing NumPy arrays for the corresponding x or y labels raises:

ValueError: The truth value of an array with more than one element is ambiguous

The factory uses truthiness checks for these optional arguments in validation, trace construction, and annotation setup. This change checks explicitly for None, allowing NumPy arrays (and other array-like labels) while still validating their dimensions.

Regression tests cover NumPy arrays for both axes and a mismatched array length. The accepted types in the docstring and the changelog are updated as well.

Fixes #4160.

Tests

  • python -m pytest tests/test_optional/test_tools/test_figure_factory.py -q ? 34 passed
  • ruff check plotly/figure_factory/_annotated_heatmap.py tests/test_optional/test_tools/test_figure_factory.py
  • git diff --check upstream/main...HEAD

create_annotated_heatmap tested its optional x and y arguments for
truthiness rather than for None. A NumPy array has no unambiguous truth
value, so passing arrays as axis labels raised

    ValueError: The truth value of an array with more than one element
    is ambiguous. Use a.any() or a.all()

from validate_annotated_heatmap, before any heatmap was built. This is
easy to hit because z itself is documented to accept an ndarray, so
labels derived from the same data are naturally arrays too.

Compare x and y against None in the three places that gate on them:
the length validation, the choice of trace/layout with or without tick
labels, and the default axis ranges in _AnnotatedHeatmap. Sequences
that are merely falsy, such as an empty list, are now length-checked
against z instead of being silently ignored.

pandas Series and Index objects work for the same reason.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

1 participant