Skip to content

Commit 52fa598

Browse files
Share per-path style helper between path collection renderers
1 parent e2e3238 commit 52fa598

1 file changed

Lines changed: 23 additions & 27 deletions

File tree

‎plotly/matplotlylib/renderer.py‎

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,24 @@ def _export_color(color):
2727
return [_export_color(c) for c in color]
2828

2929

30+
def _per_path(values, i, default):
31+
"""Return the style value for the i-th path of a path collection.
32+
33+
A scalar (or string) value applies to every path, a sequence is cycled
34+
through like matplotlib does, and None or an empty sequence yields
35+
`default`.
36+
"""
37+
if isinstance(values, str):
38+
return values
39+
if values is None:
40+
return default
41+
try:
42+
n = len(values)
43+
except TypeError:
44+
return values
45+
return values[i % n] if n else default
46+
47+
3048
class PlotlyRenderer(Renderer):
3149
"""A renderer class inheriting from base for rendering mpl plots in plotly.
3250
@@ -548,20 +566,9 @@ def _draw_line_collection(self, props):
548566
edgecolors = mpltools.convert_rgba_array(props["styles"]["edgecolor"])
549567
linewidths = mpltools.convert_linewidth_array(props["styles"]["linewidth"])
550568

551-
def per_path(colors, i, default):
552-
if isinstance(colors, str):
553-
return colors
554-
if colors is None:
555-
return default
556-
try:
557-
n = len(colors)
558-
except TypeError:
559-
return colors
560-
return colors[i % n] if n else default
561-
562569
for i, (verts, codes) in enumerate(props["paths"]):
563-
edgecolor = per_path(edgecolors, i, "rgba(0,0,0,0)")
564-
linewidth = per_path(linewidths, i, 0)
570+
edgecolor = _per_path(edgecolors, i, "rgba(0,0,0,0)")
571+
linewidth = _per_path(linewidths, i, 0)
565572
# a path may contain several disjoint lines (e.g. contour lines
566573
# of the same level); separate disjoint subpaths with None so
567574
# plotly does not connect them.
@@ -623,21 +630,10 @@ def _draw_filled_path_collection(self, props):
623630
edgecolors = mpltools.convert_rgba_array(props["styles"]["edgecolor"])
624631
linewidths = mpltools.convert_linewidth_array(props["styles"]["linewidth"])
625632

626-
def per_path(colors, i, default):
627-
if isinstance(colors, str):
628-
return colors
629-
if colors is None:
630-
return default
631-
try:
632-
n = len(colors)
633-
except TypeError:
634-
return colors
635-
return colors[i % n] if n else default
636-
637633
for i, (verts, codes) in enumerate(props["paths"]):
638-
facecolor = per_path(facecolors, i, "rgba(0,0,0,0)")
639-
edgecolor = per_path(edgecolors, i, "rgba(0,0,0,0)")
640-
linewidth = per_path(linewidths, i, 0)
634+
facecolor = _per_path(facecolors, i, "rgba(0,0,0,0)")
635+
edgecolor = _per_path(edgecolors, i, "rgba(0,0,0,0)")
636+
linewidth = _per_path(linewidths, i, 0)
641637
self.plotly_fig.add_trace(
642638
go.Scatter(
643639
x=self._convert_x_dates([v[0] for v in verts]),

0 commit comments

Comments
 (0)