|
| 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