Skip to content

Commit 65559a9

Browse files
committed
change qplot to xplot
put it in its own module
1 parent 0d83af5 commit 65559a9

File tree

2 files changed

+106
-3
lines changed

2 files changed

+106
-3
lines changed

roboticstoolbox/tools/__init__.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
ctraj,
1111
lspb,
1212
lspb_func,
13-
qplot,
1413
mstraj,
1514
)
1615
from roboticstoolbox.tools.numerical import jacobian_numerical, hessian_numerical
@@ -21,7 +20,7 @@
2120
rtb_load_jsonfile,
2221
rtb_path_to_datafile,
2322
)
24-
23+
from roboticstoolbox.tools.plot import xplot
2524
from roboticstoolbox.tools.params import rtb_set_param, rtb_get_param
2625

2726
__all__ = [
@@ -34,7 +33,7 @@
3433
"ctraj",
3534
"lspb",
3635
"lspb_func",
37-
"qplot",
36+
"xplot",
3837
"mtraj",
3938
"mstraj",
4039
"jsingu",

roboticstoolbox/tools/plot.py

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
4+
def xplot(
5+
x,
6+
y=None,
7+
wrist=False,
8+
unwrap=False,
9+
block=False,
10+
labels=None,
11+
loc=None,
12+
grid=True,
13+
stack=False,
14+
**kwargs,
15+
):
16+
"""
17+
Plot trajectory data
18+
19+
:param x: trajectory, one row per timestep
20+
:type x: ndarray(m,n)
21+
:param t: time vector, optional
22+
:type t: numpy ndarray, shape=(M,)
23+
:param wrist: distinguish arm and wrist joints with line styles
24+
:type wrist: bool
25+
:param unwrap: unwrap joint angles so that they smoothly increase or
26+
decrease when they pass through :math:`\pm \pi`
27+
:type unwrap: bool
28+
:param block: block until the plot is closed
29+
:type block: bool
30+
:param labels: legend labels
31+
:type labels: list of str, or single string with space separated labels
32+
:param kwargs: options passed to pyplot.plot
33+
:param loc: legend location as per pyplot.legend
34+
:type loc: str
35+
36+
This is a convenience function to plot trajectories, where each row represents one time step.
37+
38+
- ``xplot(q)`` plots the joint angles versus row number. If N==6 a
39+
conventional 6-axis robot is assumed, and the first three joints are
40+
shown as solid lines, the last three joints (wrist) are shown as dashed
41+
lines. A legend is also displayed.
42+
43+
- ``xplot(t, q)`` as above but displays the joint angle trajectory versus
44+
time given the time vector T (Mx1).
45+
46+
Example::
47+
48+
>>> qplot(q, x, labels='x y z')
49+
50+
:seealso: :func:`jtraj`, :func:`numpy.unwrap`
51+
"""
52+
if y is None:
53+
q = x
54+
t = np.arange(0, q.shape[0])
55+
else:
56+
t = x
57+
q = y
58+
59+
if t.ndim != 1 or q.shape[0] != t.shape[0]:
60+
raise ValueError("dimensions of arguments are not consistent")
61+
62+
if unwrap:
63+
q = np.unwrap(q, axis=0)
64+
65+
n = q.shape[1]
66+
67+
if labels is None:
68+
labels = [f"q{i}" for i in range(n)]
69+
elif isinstance(labels, str):
70+
labels = labels.split(" ")
71+
elif not isinstance(labels, (tuple, list)):
72+
raise TypeError("wrong type for labels")
73+
74+
fig, ax = plt.subplots()
75+
76+
if stack:
77+
for i in range(n):
78+
ax = plt.subplot(n, 1, i + 1)
79+
80+
plt.plot(t, q[:, i], **kwargs)
81+
82+
plt.grid(grid)
83+
ax.set_ylabel(labels[i])
84+
ax.set_xlim(t[0], t[-1])
85+
86+
ax.set_xlabel("Time (s)")
87+
88+
else:
89+
if n == 6 and wrist:
90+
plt.plot(t, q[:, 0:3], **kwargs)
91+
plt.plot(t, q[:, 3:6], "--", **kwargs)
92+
else:
93+
plt.plot(t, q, **kwargs)
94+
95+
ax.legend(labels, loc=loc)
96+
97+
plt.grid(grid)
98+
ax.set_xlabel("Time (s)")
99+
ax.set_ylabel("Joint coordinates (rad,m)")
100+
ax.set_xlim(t[0], t[-1])
101+
102+
plt.show(block=block)
103+
104+
return fig.get_axes()

0 commit comments

Comments
 (0)