kwplot.mpl_core module

Extensions of pyplot functionality. Main improvements are

kwplot.mpl_core.next_fnum(new_base=None)[source]
kwplot.mpl_core.ensure_fnum(fnum)[source]
kwplot.mpl_core.figure(fnum=None, pnum=(1, 1, 1), title=None, figtitle=None, doclf=False, docla=False, projection=None, **kwargs)[source]

http://matplotlib.org/users/gridspec.html

Parameters
  • fnum (int) – fignum = figure number

  • pnum (int, str, or tuple(int, int, int)) – plotnum = plot tuple

  • title (str) – (default = None)

  • figtitle (None) – (default = None)

  • docla (bool) – (default = False)

  • doclf (bool) – (default = False)

Returns

fig

Return type

mpl.figure.Figure

Example

>>> import kwplot
>>> kwplot.autompl()
>>> import matplotlib.pyplot as plt
>>> fnum = 1
>>> fig = figure(fnum, (2, 2, 1))
>>> fig.gca().text(0.5, 0.5, "ax1", va="center", ha="center")
>>> fig = figure(fnum, (2, 2, 2))
>>> fig.gca().text(0.5, 0.5, "ax2", va="center", ha="center")
>>> show_if_requested()

Example

>>> import kwplot
>>> kwplot.autompl()
>>> import matplotlib.pyplot as plt
>>> fnum = 1
>>> fig = figure(fnum, (2, 2, 1))
>>> fig.gca().text(0.5, 0.5, "ax1", va="center", ha="center")
>>> fig = figure(fnum, (2, 2, 2))
>>> fig.gca().text(0.5, 0.5, "ax2", va="center", ha="center")
>>> fig = figure(fnum, (2, 4, (1, slice(1, None))))
>>> fig.gca().text(0.5, 0.5, "ax3", va="center", ha="center")
>>> show_if_requested()
kwplot.mpl_core.legend(loc='best', fontproperties=None, size=None, fc='w', alpha=1, ax=None, handles=None)[source]
Parameters
  • loc (str) – (default = ‘best’) one of ‘best’, ‘upper right’, ‘upper left’, ‘lower left’, ‘lower right’, ‘right’, ‘center left’, ‘center right’, ‘lower center’, or ‘upper center’.

  • fontproperties (None) – (default = None)

  • size (None) – (default = None)

kwplot.mpl_core.show_if_requested(N=1)[source]

Used at the end of tests. Handles command line arguments for saving figures

Referencse:

http://stackoverflow.com/questions/4325733/save-a-subplot-in-matplotlib

kwplot.mpl_core.imshow(img, fnum=None, pnum=None, xlabel=None, title=None, figtitle=None, ax=None, norm=None, cmap=None, data_colorbar=False, colorspace='rgb', interpolation='nearest', alpha=None, show_ticks=False, **kwargs)[source]
Parameters
  • img (ndarray) – image data. Height, Width, and Channel dimensions can either be in standard (H, W, C) format or in (C, H, W) format. If C in [3, 4], we assume data is in the rgb / rgba colorspace by default.

  • colorspace (str) – if the data is 3-4 channels, this indicates the colorspace 1 channel data is assumed grayscale. 4 channels assumes alpha.

  • interpolation (str) – either nearest (default), bicubic, bilinear

  • norm (bool) – if True, normalizes the image intensities to fit in a colormap.

  • cmap (mpl.colors.Colormap | None) – color map used if data is not starndard image data

  • data_colorbar (bool) – if True, displays a color scale indicating how colors map to image intensities.

  • fnum (int | None) – figure number

  • pnum (tuple | None) – plot number

  • xlabel (str | None) – sets the label for the x axis

  • title (str | None) – set axes title (if ax is not given)

  • figtitle (str | None) – set figure title (if ax is not given)

  • ax (mpl.axes.Axes | None) – axes to draw on (alternative to fnum and pnum)

  • **kwargs – docla, doclf, projection

Returns

(fig, ax)

Return type

tuple

kwplot.mpl_core.set_figtitle(figtitle, subtitle='', forcefignum=True, incanvas=True, size=None, fontfamily=None, fontweight=None, fig=None)[source]

A wrapper around subtitle that also sets the canvas window title if using a Qt backend.

Parameters
  • figtitle (str)

  • subtitle (str)

  • forcefignum (bool) – (default = True)

  • incanvas (bool) – (default = True)

  • fontfamily (None) – (default = None)

  • fontweight (None) – (default = None)

  • size (None) – (default = None)

  • fig (None) – (default = None)

CommandLine

python -m kwplot.mpl_core set_figtitle --show

Example

>>> # DISABLE_DOCTEST
>>> autompl()
>>> fig = figure(fnum=1, doclf=True)
>>> result = set_figtitle(figtitle='figtitle', fig=fig)
>>> # xdoc: +REQUIRES(--show)
>>> show_if_requested()
kwplot.mpl_core.distinct_markers(num, style='astrisk', total=None, offset=0)[source]

Creates distinct marker codes (as best as possible)

Parameters
  • num (int) – number of markers to make

  • style (str) – mplt style code

  • total (int) – alternative to num

  • offset (float) – angle offset

Returns

marker codes

Return type

List[Tuple]

Example

>>> import kwplot
>>> plt = kwplot.autoplt()
>>> style = 'astrisk'
>>> marker_list = kwplot.distinct_markers(10, style)
>>> print('marker_list = {}'.format(ub.repr2(marker_list, nl=1)))
>>> x_data = np.arange(0, 3)
>>> for count, (marker) in enumerate(marker_list):
>>>     plt.plot(x_data, [count] * len(x_data), marker=marker, markersize=10, linestyle='', label=str(marker))
>>> plt.legend()
>>> kwplot.show_if_requested()
kwplot.mpl_core.distinct_colors(N, brightness=0.878, randomize=True, hue_range=(0.0, 1.0), cmap_seed=None)[source]
Parameters
  • N (int)

  • brightness (float)

Returns

RGB_tuples

Return type

list

Todo

  • [ ] This is VERY old code that needs massive cleanup.

CommandLine

python -m color_funcs --test-distinct_colors --N 2 --show --hue-range=0.05,.95
python -m color_funcs --test-distinct_colors --N 3 --show --hue-range=0.05,.95
python -m color_funcs --test-distinct_colors --N 4 --show --hue-range=0.05,.95
python -m .color_funcs --test-distinct_colors --N 3 --show --no-randomize
python -m .color_funcs --test-distinct_colors --N 4 --show --no-randomize
python -m .color_funcs --test-distinct_colors --N 6 --show --no-randomize
python -m .color_funcs --test-distinct_colors --N 20 --show

References

http://blog.jianhuashao.com/2011/09/generate-n-distinct-colors.html

CommandLine

python -m .color_funcs --exec-distinct_colors --show
python -m .color_funcs --exec-distinct_colors --show --no-randomize --N 50
python -m .color_funcs --exec-distinct_colors --show --cmap_seed=foobar
kwplot.mpl_core.phantom_legend(label_to_color, mode='line', ax=None, legend_id=None, loc=0)[source]

Creates a legend on an axis based on a label-to-color map.

Parameters

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

Todo

  • [ ] More docs and ensure this exists in the right place

kwplot.mpl_core.close_figures(figures=None)[source]

Close specified figures. If no figures are specified, close all figure.

Parameters

figures (List[mpl.figure.Figure]) – list of figures to close

kwplot.mpl_core.all_figures()[source]

Return a list of all open figures

Returns

list of all figures

Return type

List[mpl.figure.Figure]