kwplot.mpl_make module

DEPRECATED: use kwimage versions instead

Functions used to explicitly make images as ndarrays using mpl/cv2 utilities

kwplot.mpl_make.make_heatmask(probs, cmap='plasma', with_alpha=1.0, space='rgb', dsize=None)[source]

Colorizes a single-channel intensity mask (with an alpha channel)

Parameters
  • probs (ndarray) – 2D probability map with values between 0 and 1

  • cmap (str) – mpl colormap

  • with_alpha (float) – between 0 and 1, uses probs as the alpha multipled by this number.

  • space (str) – output colorspace

  • dsize (tuple) – if not None, then output is resized to W,H=dsize

SeeAlso:

kwimage.overlay_alpha_images

Example

>>> # xdoc: +REQUIRES(module:matplotlib)
>>> from kwimage.im_draw import *  # NOQA
>>> probs = np.tile(np.linspace(0, 1, 10), (10, 1))
>>> heatmask = make_heatmask(probs, with_alpha=0.8, dsize=(100, 100))
>>> # xdoc: +REQUIRES(--show)
>>> import kwplot
>>> kwplot.imshow(heatmask, fnum=1, doclf=True, colorspace='rgb')
>>> kwplot.show_if_requested()
kwplot.mpl_make.make_vector_field(dx, dy, stride=0.02, thresh=0.0, scale=1.0, alpha=1.0, color='strawberry', thickness=1, tipLength=0.1, line_type='aa')[source]

Create an image representing a 2D vector field.

Parameters
  • dx (ndarray) – grid of vector x components

  • dy (ndarray) – grid of vector y components

  • stride (int | float) – sparsity of vectors, int specifies stride step in pixels, a float specifies it as a percentage.

  • thresh (float) – only plot vectors with magnitude greater than thres

  • scale (float) – multiply magnitude for easier visualization

  • alpha (float) – alpha value for vectors. Non-vector regions receive 0 alpha (if False, no alpha channel is used)

  • color (str | tuple | kwimage.Color) – RGB color of the vectors

  • thickness (int, default=1) – thickness of arrows

  • tipLength (float, default=0.1) – fraction of line length

  • line_type (int) – either cv2.LINE_4, cv2.LINE_8, or cv2.LINE_AA

Returns

vec_img: an rgb/rgba image in 0-1 space

Return type

ndarray[float32]

SeeAlso:

kwimage.overlay_alpha_images

DEPRECATED USE: draw_vector_field instead

Example

>>> x, y = np.meshgrid(np.arange(512), np.arange(512))
>>> dx, dy = x - 256.01, y - 256.01
>>> radians = np.arctan2(dx, dy)
>>> mag = np.sqrt(dx ** 2 + dy ** 2)
>>> dx, dy = dx / mag, dy / mag
>>> img = make_vector_field(dx, dy, scale=10, alpha=False)
>>> # xdoctest: +REQUIRES(--show)
>>> import kwplot
>>> kwplot.autompl()
>>> kwplot.imshow(img)
>>> kwplot.show_if_requested()
kwplot.mpl_make.make_orimask(radians, mag=None, alpha=1.0)[source]

Makes a colormap in HSV space where the orientation changes color and mag changes the saturation/value.

Parameters
  • radians (ndarray) – orientation in radians

  • mag (ndarray) – magnitude (must be normalized between 0 and 1)

  • alpha (float | ndarray) – if False or None, then the image is returned without alpha if a float, then mag is scaled by this and used as the alpha channel if an ndarray, then this is explicilty set as the alpha channel

Returns

an rgb / rgba image in 01 space

Return type

ndarray[float32]

SeeAlso:

kwimage.overlay_alpha_images

Example

>>> # xdoc: +REQUIRES(module:matplotlib)
>>> from kwimage.im_draw import *  # NOQA
>>> x, y = np.meshgrid(np.arange(64), np.arange(64))
>>> dx, dy = x - 32, y - 32
>>> radians = np.arctan2(dx, dy)
>>> mag = np.sqrt(dx ** 2 + dy ** 2)
>>> orimask = make_orimask(radians, mag)
>>> # xdoc: +REQUIRES(--show)
>>> import kwplot
>>> kwplot.imshow(orimask, fnum=1, doclf=True, colorspace='rgb')
>>> kwplot.show_if_requested()
kwplot.mpl_make.make_legend_img(label_to_color, dpi=96, shape=(200, 200), mode='line', transparent=False)[source]

Makes an image of a categorical legend

Parameters

label_to_color (Dict[str, kwimage.Color]) – mapping from string label to the color.

CommandLine

xdoctest -m kwplot.mpl_make make_legend_img --show

Example

>>> # xdoctest: +REQUIRES(module:kwplot)
>>> import kwplot
>>> import kwimage
>>> label_to_color = {
>>>     'blue': kwimage.Color('blue').as01(),
>>>     'red': kwimage.Color('red').as01(),
>>>     'green': 'green',
>>>     'yellow': 'yellow',
>>>     'orangered': 'orangered',
>>> }
>>> img = make_legend_img(label_to_color, transparent=True)
>>> # xdoctest: +REQUIRES(--show)
>>> kwplot.autompl()
>>> kwplot.imshow(img)
>>> kwplot.show_if_requested()
kwplot.mpl_make.render_figure_to_image(fig, dpi=None, transparent=None, **savekw)[source]

Saves a figure as an image in memory.

Parameters
  • fig (matplotlib.figure.Figure) – figure to save

  • dpi (Optional[int | str]) – The resolution in dots per inch. If None it will default to the value savefig.dpi in the matplotlibrc file. If ‘figure’ it will set the dpi to be the value of the figure.

  • transparent (bool) – If True, the axes patches will all be transparent; the figure patch will also be transparent unless facecolor and/or edgecolor are specified via kwargs.

  • **savekw – other keywords passed to fig.savefig. Valid keywords include: facecolor, edgecolor, orientation, papertype, format, pad_inches, frameon.

Returns

an image in RGB or RGBA format.

Return type

np.ndarray

Note

Be sure to use fig.set_size_inches to an appropriate size before calling this function.

Example

>>> import kwplot
>>> fig = kwplot.figure(fnum=1, doclf=True)
>>> ax = fig.gca()
>>> ax.cla()
>>> ax.plot([0, 10], [0, 10])
>>> canvas_rgb = kwplot.render_figure_to_image(fig, transparent=False)
>>> canvas_rgba = kwplot.render_figure_to_image(fig, transparent=True)
>>> assert canvas_rgb.shape[2] == 3
>>> assert canvas_rgba.shape[2] == 4
>>> # xdoctest: +REQUIRES(--show)
>>> kwplot.autompl()
>>> kwplot.imshow(canvas_rgb, fnum=2)
>>> kwplot.show_if_requested()