|
| 1 | +""" |
| 2 | +Matplotlib based plotting utilities |
| 3 | +""" |
| 4 | +import math |
| 5 | +import matplotlib.pyplot as plt |
| 6 | +import numpy as np |
| 7 | + |
| 8 | + |
| 9 | +def plot_arrow(x, y, yaw, arrow_length=1.0, |
| 10 | + origin_point_plot_style="xr", |
| 11 | + head_width=0.1, fc="r", ec="k", **kwargs): |
| 12 | + """ |
| 13 | + Plot an arrow or arrows based on 2D state (x, y, yaw) |
| 14 | +
|
| 15 | + All optional settings of matplotlib.pyplot.arrow can be used. |
| 16 | + - matplotlib.pyplot.arrow: |
| 17 | + https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.arrow.html |
| 18 | +
|
| 19 | + Parameters |
| 20 | + ---------- |
| 21 | + x : a float or array_like |
| 22 | + a value or a list of arrow origin x position. |
| 23 | + y : a float or array_like |
| 24 | + a value or a list of arrow origin y position. |
| 25 | + yaw : a float or array_like |
| 26 | + a value or a list of arrow yaw angle (orientation). |
| 27 | + arrow_length : a float (optional) |
| 28 | + arrow length. default is 1.0 |
| 29 | + origin_point_plot_style : str (optional) |
| 30 | + origin point plot style. If None, not plotting. |
| 31 | + head_width : a float (optional) |
| 32 | + arrow head width. default is 0.1 |
| 33 | + fc : string (optional) |
| 34 | + face color |
| 35 | + ec : string (optional) |
| 36 | + edge color |
| 37 | + """ |
| 38 | + if not isinstance(x, float): |
| 39 | + for (i_x, i_y, i_yaw) in zip(x, y, yaw): |
| 40 | + plot_arrow(i_x, i_y, i_yaw, head_width=head_width, |
| 41 | + fc=fc, ec=ec, **kwargs) |
| 42 | + else: |
| 43 | + plt.arrow(x, y, |
| 44 | + arrow_length * math.cos(yaw), |
| 45 | + arrow_length * math.sin(yaw), |
| 46 | + head_width=head_width, |
| 47 | + fc=fc, ec=ec, |
| 48 | + **kwargs) |
| 49 | + if origin_point_plot_style is not None: |
| 50 | + plt.plot(x, y, origin_point_plot_style) |
| 51 | + |
| 52 | + |
| 53 | +def plot_curvature(x_list, y_list, heading_list, curvature, |
| 54 | + k=0.01, c="-c", label="Curvature"): |
| 55 | + """ |
| 56 | + Plot curvature on 2D path. This plot is a line from the original path, |
| 57 | + the lateral distance from the original path shows curvature magnitude. |
| 58 | + Left turning shows right side plot, right turning shows left side plot. |
| 59 | + For straight path, the curvature plot will be on the path, because |
| 60 | + curvature is 0 on the straight path. |
| 61 | +
|
| 62 | + Parameters |
| 63 | + ---------- |
| 64 | + x_list : array_like |
| 65 | + x position list of the path |
| 66 | + y_list : array_like |
| 67 | + y position list of the path |
| 68 | + heading_list : array_like |
| 69 | + heading list of the path |
| 70 | + curvature : array_like |
| 71 | + curvature list of the path |
| 72 | + k : float |
| 73 | + curvature scale factor to calculate distance from the original path |
| 74 | + c : string |
| 75 | + color of the plot |
| 76 | + label : string |
| 77 | + label of the plot |
| 78 | + """ |
| 79 | + cx = [x + d * k * np.cos(yaw - np.pi / 2.0) for x, y, yaw, d in |
| 80 | + zip(x_list, y_list, heading_list, curvature)] |
| 81 | + cy = [y + d * k * np.sin(yaw - np.pi / 2.0) for x, y, yaw, d in |
| 82 | + zip(x_list, y_list, heading_list, curvature)] |
| 83 | + |
| 84 | + plt.plot(cx, cy, c, label=label) |
| 85 | + for ix, iy, icx, icy in zip(x_list, y_list, cx, cy): |
| 86 | + plt.plot([ix, icx], [iy, icy], c) |
| 87 | + |
0 commit comments