Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
### Fixed
- Fix `hex_to_rgb` parsing of 3-digit shorthand hexadecimal colors such as `#FFF` [[#5662](https://github.com/plotly/plotly.py/pull/5662)], with thanks to @genrichez for the contribution!
- Add `<!doctype html>` to the `to_html()` template to comply with modern web standards [[#5693](https://github.com/plotly/plotly.py/pull/5693)], with thanks to @mishrakushal for the contribution!
- Accept NumPy arrays for the `x` and `y` arguments of `figure_factory.create_annotated_heatmap`, which previously raised `ValueError: The truth value of an array with more than one element is ambiguous` [[#4160](https://github.com/plotly/plotly.py/issues/4160)]


## [6.9.0] - 2026-07-09
Expand Down
14 changes: 7 additions & 7 deletions plotly/figure_factory/_annotated_heatmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ def validate_annotated_heatmap(z, x, y, annotation_text):
"z and text should have the same dimensions"
)

if x:
if x is not None:
if len(x) != len(z[0]):
raise exceptions.PlotlyError(
"oops, the x list that you "
"provided does not match the "
"width of your z matrix "
)

if y:
if y is not None:
if len(y) != len(z):
raise exceptions.PlotlyError(
"oops, the y list that you "
Expand Down Expand Up @@ -65,8 +65,8 @@ def create_annotated_heatmap(
This function adds annotations to each cell of the heatmap.

:param (list[list]|ndarray) z: z matrix to create heatmap.
:param (list) x: x axis labels.
:param (list) y: y axis labels.
:param (list|ndarray) x: x axis labels.
:param (list|ndarray) y: y axis labels.
:param (list[list]|ndarray) annotation_text: Text strings for
annotations. Should have the same dimensions as the z matrix. If no
text is added, the values of the z matrix are annotated. Default =
Expand Down Expand Up @@ -109,7 +109,7 @@ def create_annotated_heatmap(
z, x, y, annotation_text, colorscale, font_colors, reversescale, **kwargs
).make_annotations()

if x or y:
if x is not None or y is not None:
trace = dict(
type="heatmap",
z=z,
Expand Down Expand Up @@ -174,11 +174,11 @@ def __init__(
self, z, x, y, annotation_text, colorscale, font_colors, reversescale, **kwargs
):
self.z = z
if x:
if x is not None:
self.x = x
else:
self.x = range(len(z[0]))
if y:
if y is not None:
self.y = y
else:
self.y = range(len(z))
Expand Down
20 changes: 20 additions & 0 deletions tests/test_optional/test_tools/test_figure_factory.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import math

import datetime
import numpy as np
import plotly.figure_factory as ff

from plotly.exceptions import PlotlyError
Expand Down Expand Up @@ -781,6 +782,25 @@ def test_incorrect_y_size(self):
kwargs = {"z": [[1, 2], [1, 2]], "y": [1, 2, 3]}
self.assertRaises(PlotlyError, ff.create_annotated_heatmap, **kwargs)

def test_numpy_x_and_y(self):
# check: numpy arrays are accepted as x and y axis labels

a_heat = ff.create_annotated_heatmap(
[[1, 2], [3, 4]], x=np.array(["A", "B"]), y=np.array(["C", "D"])
)

self.assertEqual(list(a_heat["data"][0]["x"]), ["A", "B"])
self.assertEqual(list(a_heat["data"][0]["y"]), ["C", "D"])
# tick labels are shown when x and y are supplied
self.assertNotEqual(a_heat["layout"]["xaxis"]["showticklabels"], False)
self.assertNotEqual(a_heat["layout"]["yaxis"]["showticklabels"], False)

def test_numpy_x_wrong_size(self):
# check: PlotlyError if a numpy x is the wrong size

kwargs = {"z": [[1, 2], [1, 2]], "x": np.array(["A", "B", "C"])}
self.assertRaises(PlotlyError, ff.create_annotated_heatmap, **kwargs)

def test_simple_annotated_heatmap(self):
# we should be able to create a heatmap with annotated values with a
# logical text color
Expand Down