Skip to content

Commit 5de8217

Browse files
PR feedback
1 parent 53c1829 commit 5de8217

File tree

4 files changed

+51
-24
lines changed

4 files changed

+51
-24
lines changed

Diff for: packages/python/plotly/plotly/express/__init__.py

+3
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@
5353
set_mapbox_access_token,
5454
defaults,
5555
get_trendline_results,
56+
)
57+
58+
from ._special_inputs import ( # noqa: F401
5659
IdentityMap,
5760
Constant,
5861
)

Diff for: packages/python/plotly/plotly/express/_core.py

+6-24
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import plotly.graph_objs as go
22
import plotly.io as pio
33
from collections import namedtuple, OrderedDict
4+
from ._special_inputs import IdentityMap, Constant
45

56
from _plotly_utils.basevalidators import ColorscaleValidator
67
from .colors import qualitative, sequential
@@ -42,29 +43,6 @@ def __init__(self):
4243
del PxDefaults
4344

4445

45-
class IdentityMap(object):
46-
"""
47-
`dict`-like object which can be passed in to arguments like `color_discrete_map` to
48-
use the provided data values as colors, rather than mapping them to colors cycled
49-
from `color_discrete_sequence`.
50-
"""
51-
52-
def __getitem__(self, key):
53-
return key
54-
55-
def __contains__(self, key):
56-
return True
57-
58-
def copy(self):
59-
return self
60-
61-
62-
class Constant(object):
63-
def __init__(self, value, label=None):
64-
self.value = value
65-
self.label = label
66-
67-
6846
MAPBOX_TOKEN = None
6947

7048

@@ -161,11 +139,15 @@ def make_mapping(args, variable):
161139
if variable == "dash":
162140
arg_name = "line_dash"
163141
vprefix = "line_dash"
142+
if args[vprefix + "_map"] == "identity":
143+
val_map = IdentityMap()
144+
else:
145+
val_map = args[vprefix + "_map"].copy()
164146
return Mapping(
165147
show_in_trace_name=True,
166148
variable=variable,
167149
grouper=args[arg_name],
168-
val_map=args[vprefix + "_map"].copy(),
150+
val_map=val_map,
169151
sequence=args[vprefix + "_sequence"],
170152
updater=lambda trace, v: trace.update({parent: {variable: v}}),
171153
facet=None,
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
class IdentityMap(object):
3+
"""
4+
`dict`-like object which acts as if the value for any key is the key itself. Objects
5+
of this class can be passed in to arguments like `color_discrete_map` to
6+
use the provided data values as colors, rather than mapping them to colors cycled
7+
from `color_discrete_sequence`. This works for any `_map` argument to Plotly Express
8+
functions, such as `line_dash_map` and `symbol_map`.
9+
"""
10+
11+
def __getitem__(self, key):
12+
return key
13+
14+
def __contains__(self, key):
15+
return True
16+
17+
def copy(self):
18+
return self
19+
20+
21+
class Constant(object):
22+
"""
23+
Objects of this class can be passed to Plotly Express functions that expect column
24+
identifiers or list-like objects to indicate that this attribute should take on a
25+
constant value. An optional label can be provided.
26+
"""
27+
def __init__(self, value, label=None):
28+
self.value = value
29+
self.label = label

Diff for: packages/python/plotly/plotly/tests/test_core/test_px/test_px_input.py

+13
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,19 @@ def test_identity_map():
339339
assert "symbol=" in fig.data[0].hovertemplate
340340
assert fig.layout.legend.title.text == "symbol"
341341

342+
fig = px.scatter(
343+
x=[1, 2],
344+
y=[1, 2],
345+
symbol=["a", "b"],
346+
color=["red", "blue"],
347+
color_discrete_map="identity",
348+
)
349+
assert fig.data[0].marker.color == "red"
350+
assert fig.data[1].marker.color == "blue"
351+
assert "color=" not in fig.data[0].hovertemplate
352+
assert "symbol=" in fig.data[0].hovertemplate
353+
assert fig.layout.legend.title.text == "symbol"
354+
342355

343356
def test_constants():
344357
fig = px.scatter(x=px.Constant(1), y=[1, 2])

0 commit comments

Comments
 (0)