Skip to content

Commit 2be9a10

Browse files
committed
feat: enhance contour builder with style parameters
1 parent fd5d646 commit 2be9a10

4 files changed

Lines changed: 66 additions & 3 deletions

File tree

plotpy/builder/image.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -826,6 +826,9 @@ def contours(
826826
levels: float | np.ndarray,
827827
X: np.ndarray | None = None,
828828
Y: np.ndarray | None = None,
829+
color: str | None = None,
830+
linestyle: str | None = None,
831+
linewidth: float | None = None,
829832
) -> list[ContourItem]:
830833
"""Make a contour curves
831834
@@ -845,11 +848,24 @@ def contours(
845848
``numpy.meshgrid``), or it must both be 1-D such that ``len(Y) == N``
846849
is the number of rows in *Z*.
847850
If none, they are assumed to be integer indices, i.e. ``Y = range(N)``.
851+
color: contour color name. Default is None
852+
linestyle: contour line style (MATLAB-like string or "SolidLine",
853+
"DashLine", "DotLine", "DashDotLine", "DashDotDotLine", "NoPen").
854+
Default is None
855+
linewidth: contour line width (pixels). Default is None
848856
849857
Returns:
850858
list of :py:class:`.ContourItem` objects
851859
"""
852-
return create_contour_items(Z, levels, X, Y)
860+
return create_contour_items(
861+
Z,
862+
levels,
863+
X,
864+
Y,
865+
color=color,
866+
linestyle=linestyle,
867+
linewidth=linewidth,
868+
)
853869

854870
def histogram2D(
855871
self,

plotpy/items/contour.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
from plotpy.config import _
3131
from plotpy.items.shape.polygon import PolygonShape
32-
from plotpy.styles import ShapeParam
32+
from plotpy.styles import COLORS, ShapeParam
3333

3434

3535
class ContourLine(gds.DataSet):
@@ -123,6 +123,9 @@ def create_contour_items(
123123
levels: float | np.ndarray,
124124
X: np.ndarray | None = None,
125125
Y: np.ndarray | None = None,
126+
color: str | None = None,
127+
linestyle: str | None = None,
128+
linewidth: float | None = None,
126129
) -> list[ContourItem]:
127130
"""Create contour items
128131
@@ -142,6 +145,11 @@ def create_contour_items(
142145
``numpy.meshgrid``), or it must both be 1-D such that ``len(Y) == N``
143146
is the number of rows in *Z*.
144147
If none, they are assumed to be integer indices, i.e. ``Y = range(N)``.
148+
color: contour color name. Default is None
149+
linestyle: contour line style (MATLAB-like string or "SolidLine",
150+
"DashLine", "DotLine", "DashDotLine", "DashDotDotLine", "NoPen").
151+
Default is None
152+
linewidth: contour line width (pixels). Default is None
145153
146154
Returns:
147155
A list of :py:class:`.ContourItem` instances.
@@ -153,6 +161,14 @@ def create_contour_items(
153161
param = ShapeParam("Contour", icon="contour.png")
154162
item = ContourItem(points=cline.vertices, shapeparam=param)
155163
item.set_style("plot", "shape/contour")
164+
if color is not None:
165+
item.shapeparam.line.color = COLORS.get(color, color)
166+
if linestyle is not None:
167+
item.shapeparam.line.set_style_from_matlab(linestyle)
168+
if linewidth is not None:
169+
item.shapeparam.line.width = linewidth
170+
if color is not None or linestyle is not None or linewidth is not None:
171+
item.shapeparam.update_item(item)
156172
item.setTitle(_("Contour") + f"[Z={cline.level}]")
157173
items.append(item)
158174
return items

plotpy/tests/items/test_contours.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,13 @@ def test_contours():
2828
"""Contour plotting on two nearby 2D Gaussians"""
2929
with qt_app_context(exec_loop=True):
3030
image = create_two_gaussians()
31-
contour_items = make.contours(image, np.linspace(0.2, 1.0, 5))
31+
contour_items = make.contours(
32+
image,
33+
np.linspace(0.2, 1.0, 5),
34+
color="y",
35+
linestyle=":",
36+
linewidth=2.0,
37+
)
3238

3339
assert contour_items
3440
assert all(isinstance(item, ContourItem) for item in contour_items)

plotpy/tests/unit/test_contour.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import unittest
22

33
import numpy as np
4+
from qtpy.QtCore import Qt
45

6+
from plotpy.builder import make
57
from plotpy.items.contour import compute_contours
68
from plotpy.tests.data import gen_xyz_data
79

@@ -54,3 +56,26 @@ def test_contour_abs_level_1(self):
5456
for line in lines:
5557
assert np.all(np.equal(line.vertices[0], line.vertices[-1]))
5658
assert line.level == 1.0
59+
60+
def test_contour_item_style(self):
61+
"""Test contour builder line style parameters."""
62+
items = make.contours(
63+
self.Z,
64+
1.0,
65+
self.X,
66+
self.Y,
67+
color="r",
68+
linestyle="--",
69+
linewidth=2.5,
70+
)
71+
assert items
72+
for item in items:
73+
assert item.pen.color().name() == "#ff0000"
74+
assert item.pen.style() == Qt.DashLine
75+
assert item.pen.widthF() == 2.5
76+
77+
default_item = make.contours(self.Z, 1.0, self.X, self.Y)[0]
78+
color_item = make.contours(self.Z, 1.0, self.X, self.Y, color="b")[0]
79+
assert color_item.pen.color().name() == "#0000ff"
80+
assert color_item.pen.style() == default_item.pen.style()
81+
assert color_item.pen.widthF() == default_item.pen.widthF()

0 commit comments

Comments
 (0)