|
6 | 6 | import matplotlib.pyplot as plt
|
7 | 7 | import numpy as np
|
8 | 8 | from matplotlib import __version__, figure, interactive
|
| 9 | +from matplotlib._enums import CapStyle |
9 | 10 | from matplotlib.backend_bases import (
|
10 | 11 | FigureManagerBase,
|
11 | 12 | GraphicsContextBase,
|
@@ -146,12 +147,31 @@ def restore(self):
|
146 | 147 | self.renderer.ctx.restore()
|
147 | 148 |
|
148 | 149 | def set_capstyle(self, cs):
|
| 150 | + """ |
| 151 | + Set the cap style for lines in the graphics context. |
| 152 | +
|
| 153 | + Parameters |
| 154 | + ---------- |
| 155 | + cs : CapStyle or str |
| 156 | + The cap style to use. Can be a CapStyle enum value or a string |
| 157 | + that can be converted to a CapStyle. |
| 158 | + """ |
| 159 | + if isinstance(cs, str): |
| 160 | + cs = CapStyle(cs) |
| 161 | + |
| 162 | + # Convert the JoinStyle enum to its name if needed |
| 163 | + if hasattr(cs, "name"): |
| 164 | + cs = cs.name.lower() |
| 165 | + |
149 | 166 | if cs in ["butt", "round", "projecting"]:
|
150 | 167 | self._capstyle = cs
|
151 | 168 | self.renderer.ctx.lineCap = _capstyle_d[cs]
|
152 | 169 | else:
|
153 | 170 | raise ValueError(f"Unrecognized cap style. Found {cs}")
|
154 | 171 |
|
| 172 | + def get_capstyle(self): |
| 173 | + return self._capstyle |
| 174 | + |
155 | 175 | def set_clip_rectangle(self, rectangle):
|
156 | 176 | self.renderer.ctx.save()
|
157 | 177 | if not rectangle:
|
@@ -297,8 +317,12 @@ def _set_style(self, gc, rgbFace=None):
|
297 | 317 | rgbFace, gc.get_alpha(), gc.get_forced_alpha()
|
298 | 318 | )
|
299 | 319 |
|
300 |
| - if gc.get_capstyle(): |
301 |
| - self.ctx.lineCap = _capstyle_d[gc.get_capstyle()] |
| 320 | + capstyle = gc.get_capstyle() |
| 321 | + if capstyle: |
| 322 | + # Get the string name if it's an enum |
| 323 | + if hasattr(capstyle, "name"): |
| 324 | + capstyle = capstyle.name.lower() |
| 325 | + self.ctx.lineCap = _capstyle_d[capstyle] |
302 | 326 |
|
303 | 327 | self.ctx.strokeStyle = self._matplotlib_color_to_CSS(
|
304 | 328 | gc.get_rgb(), gc.get_alpha(), gc.get_forced_alpha()
|
|
0 commit comments