Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hover format or skip #2377

Merged
merged 23 commits into from
Apr 21, 2020
Merged
Show file tree
Hide file tree
Changes from 4 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
20 changes: 19 additions & 1 deletion packages/python/plotly/plotly/express/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,10 @@ def make_trace_kwargs(args, trace_spec, trace_data, mapping_labels, sizeref):
go.Histogram2d,
go.Histogram2dContour,
]:
hover_is_dict = isinstance(attr_value, dict)
for col in attr_value:
if hover_is_dict and not attr_value[col]:
continue
try:
position = args["custom_data"].index(col)
except (ValueError, AttributeError, KeyError):
Expand Down Expand Up @@ -387,7 +390,20 @@ def make_trace_kwargs(args, trace_spec, trace_data, mapping_labels, sizeref):
go.Parcoords,
go.Parcats,
]:
hover_lines = [k + "=" + v for k, v in mapping_labels.items()]
# Modify mapping_labels according to hover_data keys
# if hover_data is a dict
mapping_labels_copy = OrderedDict(mapping_labels)
nicolaskruchten marked this conversation as resolved.
Show resolved Hide resolved
if args["hover_data"] and isinstance(args["hover_data"], dict):
for k, v in mapping_labels.items():
if k in args["hover_data"]:
if args["hover_data"][k]:
if isinstance(args["hover_data"][k], str):
mapping_labels_copy[k] = v.replace(
"}", ":%s}" % args["hover_data"][k]
)
else:
_ = mapping_labels_copy.pop(k)
hover_lines = [k + "=" + v for k, v in mapping_labels_copy.items()]
trace_patch["hovertemplate"] = hover_header + "<br>".join(hover_lines)
trace_patch["hovertemplate"] += "<extra></extra>"
return trace_patch, fit_results
Expand Down Expand Up @@ -1029,6 +1045,8 @@ def build_dataframe(args, attrables, array_attrables):
# Finally, update argument with column name now that column exists
if field_name not in array_attrables:
args[field_name] = str(col_name)
elif isinstance(args[field_name], dict):
pass
else:
args[field_name][i] = str(col_name)

Expand Down
8 changes: 6 additions & 2 deletions packages/python/plotly/plotly/express/_doc.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,12 @@
"Values from this column or array_like appear in bold in the hover tooltip.",
],
hover_data=[
colref_list_type,
colref_list_desc,
"list of str or int, or Series or array-like, or dict",
"Either a list of names of columns in `data_frame`, or pandas Series,",
"or array_like objects",
"or a dict with column names as keys, with values True (for default formatting)",
nicolaskruchten marked this conversation as resolved.
Show resolved Hide resolved
"False (in order to remove this column from hover information),",
"or a formatting string, for example '.3f'."
"Values from these columns appear as extra data in the hover tooltip.",
],
custom_data=[
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import plotly.express as px
import numpy as np
import pandas as pd
import pytest
import plotly.graph_objects as go
from collections import OrderedDict # an OrderedDict is needed for Python 2


def test_skip_hover():
df = px.data.iris()
fig = px.scatter(
df,
x="petal_length",
y="petal_width",
size="species_id",
hover_data={"petal_length": None, "petal_width": None},
)
assert fig.data[0].hovertemplate == "species_id=%{marker.size}<extra></extra>"


def test_composite_hover():
df = px.data.tips()
hover_dict = OrderedDict(
{"day": False, "sex": True, "time": False, "total_bill": ".1f"}
)
fig = px.scatter(
df,
x="tip",
y="total_bill",
color="day",
facet_row="time",
hover_data=hover_dict,
)
assert (
fig.data[0].hovertemplate
== "tip=%{x}<br>total_bill=%{customdata[1]:.1f}<br>sex=%{customdata[0]}<extra></extra>"
)