10000 add method to choose link colors · TDNPP/robotics-toolbox-python@f963bc5 · GitHub
[go: up one dir, main page]

Skip to content

Commit f963bc5

Browse files
committed
add method to choose link colors
useful for STL models
1 parent 3888c01 commit f963bc5

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

roboticstoolbox/robot/Robot.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
# from roboticstoolbox.tools import xacro
1212
from pathlib import PurePath, PurePosixPath
1313
from scipy.optimize import minimize, Bounds, LinearConstraint
14+
from matplotlib import colors
15+
from matplotlib import cm
1416
from roboticstoolbox.tools.null import null
1517
from ansitable import ANSITable, Column
1618

@@ -267,6 +269,52 @@ def dyntable(self):
267269
table.row(link.name, *link._dyn2list())
268270
return str(table)
269271

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)
270318
# --------------------------------------------------------------------- #
271319
@property
272320
def name(self):

0 commit comments

Comments
 (0)
0