Skip to content

Commit 068e0f7

Browse files
committed
doco, bug fixes, tidyup
1 parent 21a4846 commit 068e0f7

1 file changed

Lines changed: 60 additions & 26 deletions

File tree

‎spatialmath/base/graphics.py‎

Lines changed: 60 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -159,16 +159,20 @@ def plot_point(pos, marker="bs", text=None, ax=None, textargs=None, **kwargs):
159159
else:
160160
handles.append(plt.plot(x, y, marker, **kwargs))
161161
if text is not None:
162+
try:
163+
xy = zip(x, y)
164+
except TypeError:
165+
xy = [(x, y)]
162166
if isinstance(text, str):
163167
# simple string, but might have format chars
164-
for i, xy in enumerate(zip(x, y)):
165-
handles.append(plt.text(xy[0], xy[1], " " + text.format(i), **textopts))
168+
for i, (x, y) in enumerate(xy):
169+
handles.append(plt.text(x, y, " " + text.format(i), **textopts))
166170
elif isinstance(text, (tuple, list)):
167-
for i, xy in enumerate(zip(x, y)):
171+
for i, (x, y) in enumerate(xy):
168172
handles.append(
169173
plt.text(
170-
xy[0],
171-
xy[1],
174+
x,
175+
y,
172176
" " + text[0].format(i, *[d[i] for d in text[1:]]),
173177
**textopts
174178
)
@@ -529,7 +533,7 @@ def plot_ellipse(
529533
resolution=40,
530534
inverted=False,
531535
ax=None,
532-
filled=None,
536+
filled=False,
533537
**kwargs
534538
):
535539
"""
@@ -748,7 +752,7 @@ def plot_ellipsoid(
748752
``plot_ellipse(E)`` draws the ellipsoid defined by :math:`x^T \mat{E} x = 0`
749753
on the current plot.
750754
751-
Example:
755+
Example::
752756
753757
H = plot_ellipse(diag([1 2]), [3 4]', 'r'); % draw red ellipse
754758
plot_ellipse(diag([1 2]), [5 6]', 'alter', H); % move the ellipse
@@ -782,8 +786,8 @@ def plot_cylinder(
782786
"""
783787
Plot a cylinder using matplotlib
784788
785-
:param radius: radius of sphere, defaults to 1
786-
:type radius: float, optional
789+
:param radius: radius of sphere
790+
:type radius: float
787791
:param height: height of cylinder in the z-direction
788792
:type height: float or array_like(2)
789793
:param resolution: number of points on circumferece, defaults to 50
@@ -1023,8 +1027,14 @@ def _axes_dimensions(ax):
10231027
elif classname == "AxesSubplot":
10241028
return 2
10251029

1030+
def axes_get_limits(ax):
1031+
return np.r_[ax.get_xlim(), ax.get_ylim()]
10261032

1027-
def axes_logic(ax, dimensions, projection="ortho"):
1033+
def axes_get_scale(ax):
1034+
limits = axes_get_limits(ax)
1035+
return max(abs(limits[1] - limits[0]), abs(limits[3] - limits[2]))
1036+
1037+
def axes_logic(ax, dimensions, projection="ortho", autoscale=True):
10281038
"""
10291039
Axis creation logic
10301040
@@ -1070,20 +1080,22 @@ def axes_logic(ax, dimensions, projection="ortho"):
10701080
# axis was given
10711081

10721082
if _axes_dimensions(ax) == dimensions:
1073-
print("use existing axes")
1083+
#print("use existing axes")
10741084
return ax
10751085
# mismatch in dimensions, create new axes
10761086
# print('create new axes')
10771087
plt.figure()
10781088
# no axis specified
10791089
if dimensions == 2:
10801090
ax = plt.axes()
1091+
if autoscale:
1092+
ax.autoscale()
10811093
else:
10821094
ax = plt.axes(projection="3d", proj_type=projection)
10831095
return ax
10841096

10851097

1086-
def plotvol2(dim, ax=None, equal=True, grid=False):
1098+
def plotvol2(dim, ax=None, equal=True, grid=False, labels=True):
10871099
"""
10881100
Create 2D plot area
10891101
@@ -1100,23 +1112,35 @@ def plotvol2(dim, ax=None, equal=True, grid=False):
11001112
* [A,B], A:B x A:B
11011113
* [A,B,C,D], A:B x C:D
11021114
1115+
================== ====== ======
1116+
input xrange yrange
1117+
================== ====== ======
1118+
A (scalar) -A:A -A:A
1119+
[A, B] A:B A:B
1120+
[A, B, C, D, E, F] A:B C:D
1121+
================== ====== ======
1122+
11031123
:seealso: :func:`plotvol3`, :func:`expand_dims`
11041124
"""
11051125
dims = expand_dims(dim, 2)
11061126
if ax is None:
11071127
ax = plt.subplot()
11081128
ax.axis(dims)
1109-
ax.set_xlabel("X")
1110-
ax.set_ylabel("Y")
1129+
if labels:
1130+
ax.set_xlabel("X")
1131+
ax.set_ylabel("Y")
11111132

11121133
if equal:
11131134
ax.set_aspect("equal")
11141135
if grid:
11151136
ax.grid(True)
1137+
1138+
# signal to related functions that plotvol set the axis limits
1139+
ax._plotvol = True
11161140
return ax
11171141

11181142

1119-
def plotvol3(dim=None, ax=None, equal=True, grid=False, projection="ortho"):
1143+
def plotvol3(dim=None, ax=None, equal=True, grid=False, labels=True, projection="ortho"):
11201144
"""
11211145
Create 3D plot volume
11221146
@@ -1129,9 +1153,13 @@ def plotvol3(dim=None, ax=None, equal=True, grid=False, projection="ortho"):
11291153
11301154
Initialize axes with dimensions given by ``dim`` which can be:
11311155
1132-
* A (scalar), -A:A x -A:A x -A:A
1133-
* [A,B], A:B x A:B x A:B
1134-
* [A,B,C,D,E,F], A:B x C:D x E:F
1156+
================== ====== ====== =======
1157+
input xrange yrange zrange
1158+
================== ====== ====== =======
1159+
A (scalar) -A:A -A:A -A:A
1160+
[A, B] A:B A:B A:B
1161+
[A, B, C, D, E, F] A:B C:D E:F
1162+
================== ====== ====== =======
11351163
11361164
:seealso: :func:`plotvol2`, :func:`expand_dims`
11371165
"""
@@ -1145,9 +1173,10 @@ def plotvol3(dim=None, ax=None, equal=True, grid=False, projection="ortho"):
11451173
ax.set_xlim3d(dims[0], dims[1])
11461174
ax.set_ylim3d(dims[2], dims[3])
11471175
ax.set_zlim3d(dims[4], dims[5])
1148-
ax.set_xlabel("X")
1149-
ax.set_ylabel("Y")
1150-
ax.set_zlabel("Z")
1176+
if labels:
1177+
ax.set_xlabel("X")
1178+
ax.set_ylabel("Y")
1179+
ax.set_zlabel("Z")
11511180

11521181
if equal:
11531182
try:
@@ -1159,15 +1188,19 @@ def plotvol3(dim=None, ax=None, equal=True, grid=False, projection="ortho"):
11591188
)
11601189
if grid:
11611190
ax.grid(True)
1191+
1192+
# signal to related functions that plotvol set the axis limits
1193+
ax._plotvol = True
11621194
return ax
11631195

11641196

11651197
def expand_dims(dim=None, nd=2):
1166-
"""[summary]
1198+
"""
1199+
Expact compact axis dimensions
11671200
1168-
:param dim: [description], defaults to None
1169-
:type dim: [type], optional
1170-
:param nd: [description], defaults to 2
1201+
:param dim: dimensions, defaults to None
1202+
:type dim: scalar, array_like(2), array_like(4), array_like(6), optional
1203+
:param nd: number of axes dimensions, defaults to 2
11711204
:type nd: int, optional
11721205
:raises ValueError: bad arguments
11731206
:return: 2d or 3d dimensions vector
@@ -1191,7 +1224,7 @@ def expand_dims(dim=None, nd=2):
11911224
if len(dim) == 1:
11921225
return np.r_[-dim, dim, -dim, dim]
11931226
elif len(dim) == 2:
1194-
return np.r_[-dim[0], dim[0], -dim[1], dim[1]]
1227+
return np.r_[dim[0], dim[1], dim[0], dim[1]]
11951228
elif len(dim) == 4:
11961229
return dim
11971230
else:
@@ -1241,3 +1274,4 @@ def isnotebook():
12411274
/ "test_graphics.py"
12421275
).read()
12431276
) # pylint: disable=exec-used
1277+

0 commit comments

Comments
 (0)