Accept NumPy axis labels in annotated heatmaps - #5695
Open
nyxst4ck wants to merge 1 commit into
Open
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
figure_factory.create_annotated_heatmap()documents NumPy arrays forz, but passing NumPy arrays for the correspondingxorylabels raises: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 passedruff check plotly/figure_factory/_annotated_heatmap.py tests/test_optional/test_tools/test_figure_factory.pygit diff --check upstream/main...HEAD