|
11 | 11 | # from roboticstoolbox.tools import xacro
|
12 | 12 | from pathlib import PurePath, PurePosixPath
|
13 | 13 | from scipy.optimize import minimize, Bounds, LinearConstraint
|
| 14 | +from matplotlib import colors |
| 15 | +from matplotlib import cm |
14 | 16 | from roboticstoolbox.tools.null import null
|
15 | 17 | from ansitable import ANSITable, Column
|
16 | 18 |
|
@@ -267,6 +269,52 @@ def dyntable(self):
|
267 | 269 | table.row(link.name, *link._dyn2list())
|
268 | 270 | return str(table)
|
269 | 271 |
|
| 272 | + |
| 273 | + |
| 274 | + def linkcolormap(self, linkcolors="viridis"): |
| 275 | + """ |
| 276 | + Create a colormap for robot joints |
| 277 | +
|
| 278 | + :param linkcolors: list of colors or colormap, defaults to "viridis" |
| 279 | + :type linkcolors: list or str, optional |
| 280 | + :return: color map |
| 281 | + :rtype: matplotlib.colors.ListedColormap |
| 282 | +
|
| 283 | + - ``cm = robot.linkcolormap()`` is an n-element colormap that gives a |
| 284 | + unique color for every link. The RGBA colors for link ``j`` are |
| 285 | + ``cm(j)``. |
| 286 | + - ``cm = robot.linkcolormap(cmap)`` as above but ``cmap`` is the name |
| 287 | + of a valid matplotlib colormap. The default, example above, is the |
| 288 | + ``viridis`` colormap. |
| 289 | + - ``cm = robot.linkcolormap(list of colors)`` as above but a |
| 290 | + colormap is created from a list of n color names given as strings, |
| 291 | + tuples or hexstrings. |
| 292 | + |
| 293 | + .. runblock:: pycon |
| 294 | +
|
| 295 | + >>> import roboticstoolbox as rtb |
| 296 | + >>> robot = rtb.models.DH.Puma560() |
| 297 | + >>> cm = robot.linkcolormap("inferno") |
| 298 | + >>> print(cm(range(6))) # cm(i) is 3rd color in colormap |
| 299 | + >>> cm = robot.linkcolormap(['red', 'g', (0,0.5,0), '#0f8040', 'yellow', 'cyan']) |
| 300 | + >>> print(cm(range(6))) |
| 301 | +
|
| 302 | + .. note:: |
| 303 | + |
| 304 | + - Colormaps have 4-elements: red, green, blue, alpha (RGBA) |
| 305 | + - Names of supported colors and colormaps are defined in the matplotlib |
| 306 | + documentation. |
| 307 | +
|
| 308 | + - `Specifying colors <https://matplotlib.org/3.1.0/tutorials/colors/colors.html#sphx-glr-tutorials-colors-colors-py>`_ |
| 309 | + - `Colormaps <https://matplotlib.org/3.1.0/tutorials/colors/colormaps.html#sphx-glr-tutorials-colors-colormaps-py>`_ |
| 310 | + """ |
| 311 | + |
| 312 | + if isinstance(linkcolors, list) and len(linkcolors) == self.n: |
| 313 | + # provided a list of color names |
| 314 | + return colors.ListedColormap(linkcolors) |
| 315 | + else: |
| 316 | + # assume it is a colormap name |
| 317 | + return cm.get_cmap(linkcolors, 6) |
270 | 318 | # --------------------------------------------------------------------- #
|
271 | 319 | @property
|
272 | 320 | def name(self):
|
|
0 commit comments