Skip to content

Commit 6732f46

Browse files
committed
Added PlotBuilder.polygon
1 parent f8d0e14 commit 6732f46

File tree

2 files changed

+32
-17
lines changed

2 files changed

+32
-17
lines changed

plotpy/builder.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
Marker,
4646
MaskedImageItem,
4747
MaskedXYImageItem,
48+
PolygonShape,
4849
QuadGridItem,
4950
RangeComputation,
5051
RangeComputation2d,
@@ -2279,6 +2280,27 @@ def ellipse(
22792280
item.set_ydiameter(x2, y2, x3, y3)
22802281
return item
22812282

2283+
def polygon(
2284+
self, x: np.ndarray, y: np.ndarray, closed: bool, title: str | None = None
2285+
) -> PolygonShape:
2286+
"""Make a polygon shape `plot item`
2287+
2288+
Args:
2289+
x: polygon x coordinates
2290+
y: polygon y coordinates
2291+
closed: closed polygon
2292+
title: label name. Default is None
2293+
2294+
Returns:
2295+
:py:class:`.PolygonShape` object
2296+
"""
2297+
points = np.array([x, y]).T
2298+
item = PolygonShape(points, closed=closed)
2299+
item.set_style("plot", "shape/drag")
2300+
if title is not None:
2301+
item.setTitle(title)
2302+
return item
2303+
22822304
def circle(
22832305
self, x0: float, y0: float, x1: float, y1: float, title: str | None = None
22842306
) -> EllipseShape:

plotpy/tests/gui/test_autoscale_shapes.py

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@
1010
import numpy as np
1111
from guidata.qthelpers import qt_app_context
1212

13-
from plotpy.items import AnnotatedRectangle, EllipseShape, PolygonShape
13+
from plotpy.builder import make
14+
from plotpy.items import PolygonShape
1415
from plotpy.plot import PlotDialog
15-
from plotpy.styles import AnnotationParam, ShapeParam
16+
from plotpy.styles import ShapeParam
1617
from plotpy.tools import (
1718
AnnotatedCircleTool,
1819
AnnotatedEllipseTool,
@@ -64,30 +65,22 @@ def test_autoscale_shapes():
6465
win.manager.get_itemlist_panel().show()
6566

6667
# Add a polygon
67-
delta = 0.025
68-
x = np.arange(-3.0, 3.0, delta)
69-
param = ShapeParam()
70-
param.label = "Polygon"
71-
crv = PolygonShape(closed=False, shapeparam=param)
72-
crv.set_points(np.column_stack((x, np.sin(x))))
68+
x = np.arange(-3.0, 3.0, 0.2)
69+
crv = make.polygon(x, np.sin(x), False, "Polygon")
7370
plot.add_item(crv)
7471

7572
# Add a circle
76-
param = ShapeParam()
77-
param.label = "Circle"
78-
circle = EllipseShape(-1, 2, shapeparam=param)
73+
circle = make.circle(-1, 2, 0, 0, "Circle")
7974
plot.add_item(circle)
8075

8176
# Add an annotated rectangle
82-
param = AnnotationParam()
83-
param.title = "Annotated rectangle"
84-
rect = AnnotatedRectangle(2.5, 1, 4, 1.2, annotationparam=param)
77+
rect = make.annotated_rectangle(2.5, 1, 4, 1.2, "Annotated rectangle")
8578
plot.add_item(rect)
8679

8780
# Add an annotated rectangle excluded
88-
param = AnnotationParam()
89-
param.title = "Annotated rectangle excluded from autoscale"
90-
rect = AnnotatedRectangle(1.0, 2.0, 5, 10, annotationparam=param)
81+
rect = make.annotated_rectangle(
82+
1.0, 2.0, 5, 10, "Annotated rectangle excluded from autoscale"
83+
)
9184
plot.add_item(rect)
9285

9386
plot.add_autoscale_excludes([rect])

0 commit comments

Comments
 (0)