add caching for scaled linetype pattern
@@ -60,6 +60,7 @@ class MatplotlibBackend(Backend):
self.point_size_relative = point_size_relative
self.font = font
self._font_measurements = _get_font_measurements(font)
+ self._line_style_pattern_cache = dict()
def _get_z(self) -> int:
z = self._current_z
@@ -74,7 +75,7 @@ class MatplotlibBackend(Backend):
Line2D(
(start.x, end.x), (start.y, end.y),
linewidth=properties.lineweight * POINTS,
- linestyle=_get_line_style_pattern(properties),
+ linestyle=self.get_line_style_pattern(properties),
color=properties.color,
zorder=self._get_z()
))
@@ -84,7 +85,7 @@ class MatplotlibBackend(Backend):
patch = PathPatch(
Path(vertices, codes),
linewidth=properties.lineweight * POINTS,
- linestyle=_get_line_style_pattern(properties),
+ linestyle=self.get_line_style_pattern(properties),
color=properties.color,
fill=bool(properties.filling),
zorder=self._get_z()
@@ -134,6 +135,14 @@ class MatplotlibBackend(Backend):
scale = cap_height / self._font_measurements.cap_height
return max(x for x, y in path.vertices) * scale
+ def get_line_style_pattern(self, properties: Properties, scale: float = 10):
+ key = (properties.linetype_name, properties.linetype_scale * scale)
+ pattern = self._line_style_pattern_cache.get(key)
+ if pattern is None:
+ pattern = _get_line_style_pattern(properties, scale)
+ self._line_style_pattern_cache[key] = pattern
+ return pattern
+
def clear(self):
self.ax.clear()
@@ -173,7 +182,7 @@ def _get_font_measurements(
)
-def _get_line_style_pattern(properties: Properties, scale: float = 10):
+def _get_line_style_pattern(properties: Properties, scale: float):
""" Return matplotlib line style tuple: (offset, on_off_sequence)
See examples: https://matplotlib.org/gallery/lines_bars_and_markers/linestyles.html
