Skip to content

Commit 7170a52

Browse files
committed
address review comments
1 parent 546dd1a commit 7170a52

File tree

4 files changed

+15
-3
lines changed

4 files changed

+15
-3
lines changed

doc/python/hover-text-and-formatting.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ fig.show()
124124
* `True` to add a different column, with default formatting
125125
* a formatting string starting with `:` for numbers [d3-format's syntax](https://github.com/d3/d3-3.x-api-reference/blob/master/Formatting.md#d3_forma), and `|` for dates in [d3-time-format's syntax](https://github.com/d3/d3-3.x-api-reference/blob/master/Time-Formatting.md#format), for example `:.3f`, `|%a`.
126126

127-
For passing new data, the value is a tuple, which first element is one of the possible values described above for existing columns, and the second element correspond to the hover values, for example `(True, [1, 2, 3])` or `(':.1f', [1.54, 2.345])`.
127+
It is also possible to pass new data as values of the `hover_data` dict, either as list-like data, or inside a tuple, which first element is one of the possible values described above for existing columns, and the second element correspond to the list-like data, for example `(True, [1, 2, 3])` or `(':.1f', [1.54, 2.345])`.
128128

129129
These different cases are illustrated in the following example.
130130

@@ -138,7 +138,7 @@ fig = px.scatter(df, x='petal_length', y='sepal_length', facet_col='species', co
138138
'petal_width':True, # add other column, default formatting
139139
'sepal_width':':.2f', # add other column, customized formatting
140140
# data not in dataframe, default formatting
141-
'suppl_1': (True, np.random.random(len(df))),
141+
'suppl_1': np.random.random(len(df)),
142142
# data not in dataframe, customized formatting
143143
'suppl_2': (':.3f', np.random.random(len(df)))
144144
})

packages/python/plotly/plotly/express/_core.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -885,6 +885,10 @@ def _get_reserved_col_names(args, attrables, array_attrables):
885885
return reserved_names
886886

887887

888+
def _isinstance_listlike(x):
889+
return isinstance(x, list) or isinstance(x, np.ndarray) or isinstance(x, pd.Series)
890+
891+
888892
def build_dataframe(args, attrables, array_attrables):
889893
"""
890894
Constructs a dataframe and modifies `args` in-place.
@@ -944,6 +948,8 @@ def build_dataframe(args, attrables, array_attrables):
944948
# If dict, convert all values of hover_data to tuples to simplify processing
945949
if hover_data_is_dict:
946950
for k in args["hover_data"]:
951+
if _isinstance_listlike(args["hover_data"][k]):
952+
args["hover_data"][k] = (True, args["hover_data"][k])
947953
if not isinstance(args["hover_data"][k], tuple):
948954
args["hover_data"][k] = (args["hover_data"][k], None)
949955

packages/python/plotly/plotly/express/_doc.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@
186186
"or a dict with column names as keys, with values True (for default formatting)",
187187
"False (in order to remove this column from hover information),",
188188
"or a formatting string, for example ':.3f' or '|%a'",
189+
"or list-like data to appear in the hover tooltip",
189190
"or tuples with a bool or formatting string as first element,",
190191
"and list-like data to appear in hover as second element",
191192
"Values from these columns appear as extra data in the hover tooltip.",

packages/python/plotly/plotly/tests/test_core/test_px/test_px_hover.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,12 @@ def test_composite_hover():
3838
assert ":.1f" in fig.data[0].hovertemplate
3939

4040

41-
def test_tuple_hover_data():
41+
def test_newdatain_hover_data():
42+
fig = px.scatter(x=[1, 2, 3], y=[3, 4, 5], hover_data={"comment": ["a", "b", "c"]})
43+
assert (
44+
fig.data[0].hovertemplate
45+
== "x=%{x}<br>y=%{y}<br>comment=%{customdata[0]}<extra></extra>"
46+
)
4247
fig = px.scatter(
4348
x=[1, 2, 3], y=[3, 4, 5], hover_data={"comment": (True, ["a", "b", "c"])}
4449
)

0 commit comments

Comments
 (0)