Skip to content

Commit de00202

Browse files
adding pattern_shape to PX
1 parent b34bd6d commit de00202

File tree

14 files changed

+93
-21
lines changed

14 files changed

+93
-21
lines changed

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

+20-3
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,9 @@ def density_heatmap(
200200
z=[
201201
"For `density_heatmap` and `density_contour` these values are used as the inputs to `histfunc`.",
202202
],
203-
histfunc=["The arguments to this function are the values of `z`.",],
203+
histfunc=[
204+
"The arguments to this function are the values of `z`.",
205+
],
204206
),
205207
)
206208

@@ -308,6 +310,7 @@ def bar(
308310
x=None,
309311
y=None,
310312
color=None,
313+
pattern_shape=None,
311314
facet_row=None,
312315
facet_col=None,
313316
facet_col_wrap=0,
@@ -329,6 +332,8 @@ def bar(
329332
color_discrete_sequence=None,
330333
color_discrete_map=None,
331334
color_continuous_scale=None,
335+
pattern_shape_sequence=None,
336+
pattern_shape_map=None,
332337
range_color=None,
333338
color_continuous_midpoint=None,
334339
opacity=None,
@@ -410,6 +415,7 @@ def histogram(
410415
x=None,
411416
y=None,
412417
color=None,
418+
pattern_shape=None,
413419
facet_row=None,
414420
facet_col=None,
415421
facet_col_wrap=0,
@@ -423,6 +429,8 @@ def histogram(
423429
labels=None,
424430
color_discrete_sequence=None,
425431
color_discrete_map=None,
432+
pattern_shape_sequence=None,
433+
pattern_shape_map=None,
426434
marginal=None,
427435
opacity=None,
428436
orientation=None,
@@ -451,7 +459,9 @@ def histogram(
451459
args=locals(),
452460
constructor=go.Histogram,
453461
trace_patch=dict(
454-
histnorm=histnorm, histfunc=histfunc, cumulative=dict(enabled=cumulative),
462+
histnorm=histnorm,
463+
histfunc=histfunc,
464+
cumulative=dict(enabled=cumulative),
455465
),
456466
layout_patch=dict(barmode=barmode, barnorm=barnorm),
457467
)
@@ -511,7 +521,11 @@ def violin(
511521
args=locals(),
512522
constructor=go.Violin,
513523
trace_patch=dict(
514-
points=points, box=dict(visible=box), scalegroup=True, x0=" ", y0=" ",
524+
points=points,
525+
box=dict(visible=box),
526+
scalegroup=True,
527+
x0=" ",
528+
y0=" ",
515529
),
516530
layout_patch=dict(violinmode=violinmode),
517531
)
@@ -892,6 +906,7 @@ def bar_polar(
892906
r=None,
893907
theta=None,
894908
color=None,
909+
pattern_shape=None,
895910
hover_name=None,
896911
hover_data=None,
897912
custom_data=None,
@@ -903,6 +918,8 @@ def bar_polar(
903918
color_discrete_sequence=None,
904919
color_discrete_map=None,
905920
color_continuous_scale=None,
921+
pattern_shape_sequence=None,
922+
pattern_shape_map=None,
906923
range_color=None,
907924
color_continuous_midpoint=None,
908925
barnorm=None,

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

+33-6
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"color", # renamed to marker.color or line.color in infer_config
3131
"symbol", # renamed to marker.symbol in infer_config
3232
"line_dash", # renamed to line.dash in infer_config
33+
"pattern_shape", # renamed to marker.pattern.shape in infer_config
3334
]
3435
all_attrables = (
3536
direct_attrables + array_attrables + group_attrables + renameable_group_attrables
@@ -51,6 +52,8 @@ class PxDefaults(object):
5152
"symbol_map",
5253
"line_dash_sequence",
5354
"line_dash_map",
55+
"pattern_shape_sequence",
56+
"pattern_shape_map",
5457
"size_max",
5558
"category_orders",
5659
"labels",
@@ -70,6 +73,8 @@ def reset(self):
7073
self.symbol_map = {}
7174
self.line_dash_sequence = None
7275
self.line_dash_map = {}
76+
self.pattern_shape_sequence = None
77+
self.pattern_shape_map = {}
7378
self.size_max = 20
7479
self.category_orders = {}
7580
self.labels = {}
@@ -206,14 +211,17 @@ def make_mapping(args, variable):
206211
updater=(lambda trace, v: v),
207212
facet="row" if variable == "facet_row" else "col",
208213
)
209-
(parent, variable) = variable.split(".")
214+
(parent, variable, *other_variables) = variable.split(".")
210215
vprefix = variable
211216
arg_name = variable
212217
if variable == "color":
213218
vprefix = "color_discrete"
214219
if variable == "dash":
215220
arg_name = "line_dash"
216221
vprefix = "line_dash"
222+
if variable == "pattern":
223+
arg_name = "pattern_shape"
224+
vprefix = "pattern_shape"
217225
if args[vprefix + "_map"] == "identity":
218226
val_map = IdentityMap()
219227
else:
@@ -224,7 +232,9 @@ def make_mapping(args, variable):
224232
grouper=args[arg_name],
225233
val_map=val_map,
226234
sequence=args[vprefix + "_sequence"],
227-
updater=lambda trace, v: trace.update({parent: {variable: v}}),
235+
updater=lambda trace, v: trace.update(
236+
{parent: {".".join([variable] + other_variables): v}}
237+
),
228238
facet=None,
229239
)
230240

@@ -952,6 +962,16 @@ def apply_default_cascade(args):
952962
"longdashdot",
953963
]
954964

965+
if "pattern_shape_sequence" in args:
966+
if args["pattern_shape_sequence"] is None and args["template"].data.bar:
967+
args["pattern_shape_sequence"] = [
968+
bar.marker.pattern.shape for bar in args["template"].data.bar
969+
]
970+
if not args["pattern_shape_sequence"] or not any(
971+
args["pattern_shape_sequence"]
972+
):
973+
args["pattern_shape_sequence"] = ["", "/", "\\", "x", "+", "."]
974+
955975

956976
def _check_name_not_reserved(field_name, reserved_names):
957977
if field_name not in reserved_names:
@@ -1691,14 +1711,15 @@ def infer_config(args, constructor, trace_patch, layout_patch):
16911711
else:
16921712
show_colorbar = False
16931713

1694-
# Compute line_dash grouping attribute
16951714
if "line_dash" in args:
16961715
grouped_attrs.append("line.dash")
16971716

1698-
# Compute symbol grouping attribute
16991717
if "symbol" in args:
17001718
grouped_attrs.append("marker.symbol")
17011719

1720+
if "pattern_shape" in args:
1721+
grouped_attrs.append("marker.pattern.shape")
1722+
17021723
if "orientation" in args:
17031724
has_x = args["x"] is not None
17041725
has_y = args["y"] is not None
@@ -1949,8 +1970,14 @@ def make_figure(args, constructor, trace_patch=None, layout_patch=None):
19491970
# this catches some odd cases like marginals
19501971
if (
19511972
trace_spec != trace_specs[0]
1952-
and trace_spec.constructor in [go.Violin, go.Box, go.Histogram]
1953-
and m.variable == "symbol"
1973+
and (
1974+
trace_spec.constructor in [go.Violin, go.Box]
1975+
and m.variable in ["symbol", "pattern"]
1976+
)
1977+
or (
1978+
trace_spec.constructor in [go.Histogram]
1979+
and m.variable in ["symbol"]
1980+
)
19541981
):
19551982
pass
19561983
elif (

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

+17
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,11 @@
182182
colref_desc,
183183
"Values from this column or array_like are used to assign symbols to marks.",
184184
],
185+
pattern_shape=[
186+
colref_type,
187+
colref_desc,
188+
"Values from this column or array_like are used to assign pattern shapes to marks.",
189+
],
185190
size=[
186191
colref_type,
187192
colref_desc,
@@ -283,6 +288,18 @@
283288
"Strings should define valid plotly.js dash-patterns.",
284289
"When `line_dash` is set, values in that column are assigned dash-patterns by cycling through `line_dash_sequence` in the order described in `category_orders`, unless the value of `line_dash` is a key in `line_dash_map`.",
285290
],
291+
pattern_shape_map=[
292+
"dict with str keys and str values (default `{}`)",
293+
"Strings values define plotly.js patterns-shapes.",
294+
"Used to override `pattern_shape_sequences` to assign a specific patterns-shapes to lines corresponding with specific values.",
295+
"Keys in `pattern_shape_map` should be values in the column denoted by `pattern_shape`.",
296+
"Alternatively, if the values of `pattern_shape` are valid patterns-shapes names, the string `'identity'` may be passed to cause them to be used directly.",
297+
],
298+
pattern_shape_sequence=[
299+
"list of str",
300+
"Strings should define valid plotly.js patterns-shapes.",
301+
"When `pattern_shape` is set, values in that column are assigned patterns-shapes by cycling through `pattern_shape_sequence` in the order described in `category_orders`, unless the value of `pattern_shape` is a key in `pattern_shape_map`.",
302+
],
286303
color_discrete_sequence=[
287304
"list of str",
288305
"Strings should define valid CSS-colors.",

0 commit comments

Comments
 (0)