From 7c98a5076c8955812c9ce2b1f3e2170e8df7bcb0 Mon Sep 17 00:00:00 2001 From: Thomas VINCENT Date: Tue, 19 Dec 2023 16:28:14 +0100 Subject: [PATCH] Add support for (0, None) and (0, ()) linestyle as a valid style for solid line --- src/silx/gui/plot/backends/BackendBase.py | 2 +- src/silx/gui/plot/backends/BackendOpenGL.py | 13 +++++++++++-- src/silx/gui/plot/items/core.py | 13 ++++++++++--- 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/silx/gui/plot/backends/BackendBase.py b/src/silx/gui/plot/backends/BackendBase.py index 241e7ca316..8d70286a8a 100755 --- a/src/silx/gui/plot/backends/BackendBase.py +++ b/src/silx/gui/plot/backends/BackendBase.py @@ -216,7 +216,7 @@ def addMarker( text: str | None, color: str, symbol: str | None, - linestyle: str | tuple[float, tuple[float, ...]], + linestyle: str | tuple[float, tuple[float, ...] | None], linewidth: float, constraint: Callable[[float, float], tuple[float, float]] | None, yaxis: str, diff --git a/src/silx/gui/plot/backends/BackendOpenGL.py b/src/silx/gui/plot/backends/BackendOpenGL.py index c3905ca72a..da2fec0a61 100755 --- a/src/silx/gui/plot/backends/BackendOpenGL.py +++ b/src/silx/gui/plot/backends/BackendOpenGL.py @@ -875,7 +875,7 @@ def _castArrayTo(v): else: raise ValueError("Unsupported data type") - _DASH_PATTERNS = { # Convert from linestyle to offset and dash pattern + _DASH_PATTERNS = { "": (0.0, None), " ": (0.0, None), "-": (0.0, ()), @@ -884,6 +884,12 @@ def _castArrayTo(v): ":": (0.0, (1, 1.65, 1, 1.65)), None: (0.0, None), } + """Convert from linestyle to (offset, (dash pattern)) + + Note: dash pattern internal convention differs from matplotlib: + - None: no line at all + - (): "solid" line + """ def _lineStyleToDashOffsetPattern( self, style @@ -892,8 +898,11 @@ def _lineStyleToDashOffsetPattern( if style is None or isinstance(style, str): return self._DASH_PATTERNS[style] - # (offset, (dash pattern)) + # (offset, (dash pattern)) case offset, pattern = style + if pattern is None: + # Convert from matplotlib to internal representation of solid + pattern = () if len(pattern) == 2: pattern = pattern * 2 return offset, pattern diff --git a/src/silx/gui/plot/items/core.py b/src/silx/gui/plot/items/core.py index a0e230314b..0c365e6728 100644 --- a/src/silx/gui/plot/items/core.py +++ b/src/silx/gui/plot/items/core.py @@ -837,6 +837,7 @@ def setSymbolSize(self, size): LineStyleType = Union[ str, + Tuple[float, None], Tuple[float, Tuple[float, float]], Tuple[float, Tuple[float, float, float, float]], ] @@ -888,9 +889,15 @@ def isValidLineStyle(cls, style: LineStyleType | None) -> bool: if ( len(style) == 2 and isinstance(style[0], float) - and isinstance(style[1], tuple) - and len(style[1]) in (2, 4) - and all(map(lambda item: isinstance(item, float), style[1])) + and ( + style[1] is None + or style[1] == () + or ( + isinstance(style[1], tuple) + and len(style[1]) in (2, 4) + and all(map(lambda item: isinstance(item, float), style[1])) + ) + ) ): return True return False