contourpy.util

This module consists of utility functions that are used internally for tests and examples. Please feel free to use them to experiment with contourpy, but you should not use them in downstream projects as they are liable to change at short notice without a formal deprecation period.

contourpy.util.data.random(shape, seed=2187, mask_fraction=0.0)[source]

Return random test data..

Parameters
  • shape (tuple(int, int)) – 2D shape of data to return.

  • seed (int, optional) – Seed for random number generator, default 2187.

  • mask_fraction (float, optional) – Fraction of elements to mask, default 0.

Returns

x, y, z test data, z will be masked if mask_fraction is greater than zero.

Return type

Tuple of 3 arrays

contourpy.util.data.simple(shape, want_mask=False)[source]

Return simple test data consisting of the sum of two gaussians.

Parameters
  • shape (tuple(int, int)) – 2D shape of data to return.

  • want_mask (bool, optional) – Whether test data should be masked or not, default False.

Returns

x, y, z test data, z will be masked if want_mask=True.

Return type

Tuple of 3 arrays

class contourpy.util.bokeh_renderer.BokehRenderer(nrows=1, ncols=1, figsize=(9, 9), show_frame=True, want_svg=False)[source]

Utility renderer using Bokeh to render a grid of plots over the same (x, y) range.

Parameters
  • nrows (int, optional) – Number of rows of plots, default 1.

  • ncols (int, optional) – Number of columns of plots, default 1.

  • figsize (tuple(float, float), optional) – Figure size in inches (assuming 100 dpi), default (9, 9).

  • show_frame (bool, optional) – Whether to show frame and axes ticks, default True.

  • want_svg (bool, optional) – Whether output is required in SVG format or not, default False.

Warning

BokehRenderer, unlike MplRenderer, needs to be told in advance if output to SVG format will be required later, otherwise it will assume PNG output.

filled(filled, fill_type, ax=0, color='C0', alpha=0.7)[source]

Plot filled contours on a single plot.

Parameters
  • filled (sequence of arrays) – Filled contour data as returned by filled().

  • fill_type (FillType) – Type of filled data, as returned by fill_type.

  • ax (int or Bokeh Figure, optional) – Which plot to use, default 0.

  • color (str, optional) – Color to plot with. May be a string color or the letter "C" followed by an integer in the range "C0" to "C9" to use a color from the Category10 palette. Default "C0".

  • alpha (float, optional) – Opacity to plot with, default 0.7.

grid(x, y, ax=0, color='black', alpha=0.1, point_color=None, quad_as_tri_alpha=0)[source]

Plot quad grid lines on a single plot.

Parameters
  • x (array-like of shape (ny, nx) or (nx,)) – The x-coordinates of the grid points.

  • y (array-like of shape (ny, nx) or (ny,)) – The y-coordinates of the grid points.

  • ax (int or Bokeh Figure, optional) – Which plot to use, default 0.

  • color (str, optional) – Color to plot grid lines, default "black".

  • alpha (float, optional) – Opacity to plot lines with, default 0.1.

  • point_color (str, optional) – Color to plot grid points or None if grid points should not be plotted, default None.

  • quad_as_tri_alpha (float, optional) – Opacity to plot quad_as_tri grid, default 0.

Colors may be a string color or the letter "C" followed by an integer in the range "C0" to "C9" to use a color from the Category10 palette.

Warning

quad_as_tri_alpha > 0 plots all quads as though they are unmasked.

lines(lines, line_type, ax=0, color='C0', alpha=1.0, linewidth=1)[source]

Plot contour lines on a single plot.

Parameters
  • lines (sequence of arrays) – Contour line data as returned by lines().

  • line_type (LineType) – Type of lines data, as returned by line_type.

  • ax (int or Bokeh Figure, optional) – Which plot to use, default 0.

  • color (str, optional) – Color to plot lines. May be a string color or the letter "C" followed by an integer in the range "C0" to "C9" to use a color from the Category10 palette. Default "C0".

  • alpha (float, optional) – Opacity to plot lines with, default 1.0.

  • linewidth (float, optional) – Width of lines, default 1.

Note

Assumes all lines are open line strips not closed line loops.

mask(x, y, z, ax=0, color='black')[source]

Plot masked out grid points as circles on a single plot.

Parameters
  • x (array-like of shape (ny, nx) or (nx,)) – The x-coordinates of the grid points.

  • y (array-like of shape (ny, nx) or (ny,)) – The y-coordinates of the grid points.

  • z (masked array of shape (ny, nx) – z-values.

  • ax (int or Bokeh Figure, optional) – Which plot to use, default 0.

  • color (str, optional) – Circle color, default "black".

save(filename, transparent=False)[source]

Save plots to SVG or PNG file.

Parameters
  • filename (str) – Filename to save to.

  • transparent (bool, optional) – Whether background should be transparent, default False.

Warning

To output to SVG file, want_svg=True must have been passed to the constructor.

save_to_buffer()[source]

Save plots to an io.BytesIO buffer.

Returns

PNG image buffer.

Return type

BytesIO

show()[source]

Show plots in web browser, in usual Bokeh manner.

title(title, ax=0, color=None)[source]

Set the title of a single plot.

Parameters
  • title (str) – Title text.

  • ax (int or Bokeh Figure, optional) – Which plot to set the title of, default 0.

  • color (str, optional) – Color to set title. May be a string color or the letter "C" followed by an integer in the range "C0" to "C9" to use a color from the Category10 palette. Default None which is black.

z_values(x, y, z, ax=0, color='green', fmt='.1f', quad_as_tri=False)[source]

Show z values on a single plot.

Parameters
  • x (array-like of shape (ny, nx) or (nx,)) – The x-coordinates of the grid points.

  • y (array-like of shape (ny, nx) or (ny,)) – The y-coordinates of the grid points.

  • z (array-like of shape (ny, nx) – z-values.

  • ax (int or Bokeh Figure, optional) – Which plot to use, default 0.

  • color (str, optional) – Color of added text. May be a string color or the letter "C" followed by an integer in the range "C0" to "C9" to use a color from the Category10 palette. Default "green".

  • fmt (str, optional) – Format to display z-values, default ".1f".

  • quad_as_tri (bool, optional) – Whether to show z-values at the quad_as_tri centres of quads.

Warning

quad_as_tri=True shows z-values for all quads, even if masked.

class contourpy.util.mpl_renderer.MplRenderer(nrows=1, ncols=1, figsize=(9, 9), show_frame=True, backend=None, gridspec_kw=None)[source]

Utility renderer using Matplotlib to render a grid of plots over the same (x, y) range.

Parameters
  • nrows (int, optional) – Number of rows of plots, default 1.

  • ncols (int, optional) – Number of columns of plots, default 1.

  • figsize (tuple(float, float), optional) – Figure size in inches, default (9, 9).

  • show_frame (bool, optional) – Whether to show frame and axes ticks, default True.

  • backend (str, optional) – Matplotlib backend to use or None for default backend. Default None.

  • gridspec_kw (dict, optional) – Gridspec keyword arguments to pass to plt.subplots, default None.

filled(filled, fill_type, ax=0, color='C0', alpha=0.7)[source]

Plot filled contours on a single Axes.

Parameters
  • filled (sequence of arrays) – Filled contour data as returned by filled().

  • fill_type (FillType) – Type of filled data, as returned by fill_type.

  • ax (int or Maplotlib Axes, optional) – Which axes to plot on, default 0.

  • color (str, optional) – Color to plot with. May be a string color or the letter "C" followed by an integer in the range "C0" to "C9" to use a color from the tab10 colormap. Default "C0".

  • alpha (float, optional) – Opacity to plot with, default 0.7.

grid(x, y, ax=0, color='black', alpha=0.1, point_color=None, quad_as_tri_alpha=0)[source]

Plot quad grid lines on a single Axes.

Parameters
  • x (array-like of shape (ny, nx) or (nx,)) – The x-coordinates of the grid points.

  • y (array-like of shape (ny, nx) or (ny,)) – The y-coordinates of the grid points.

  • ax (int or Matplotlib Axes, optional) – Which Axes to plot on, default 0.

  • color (str, optional) – Color to plot grid lines, default "black".

  • alpha (float, optional) – Opacity to plot lines with, default 0.1.

  • point_color (str, optional) – Color to plot grid points or None if grid points should not be plotted, default None.

  • quad_as_tri_alpha (float, optional) – Opacity to plot quad_as_tri grid, default 0.

Colors may be a string color or the letter "C" followed by an integer in the range "C0" to "C9" to use a color from the tab10 colormap.

Warning

quad_as_tri_alpha > 0 plots all quads as though they are unmasked.

lines(lines, line_type, ax=0, color='C0', alpha=1.0, linewidth=1)[source]

Plot contour lines on a single Axes.

Parameters
  • lines (sequence of arrays) – Contour line data as returned by lines().

  • line_type (LineType) – Type of lines data, as returned by line_type.

  • ax (int or Matplotlib Axes, optional) – Which Axes to plot on, default 0.

  • color (str, optional) – Color to plot lines. May be a string color or the letter "C" followed by an integer in the range "C0" to "C9" to use a color from the tab10 colormap. Default "C0".

  • alpha (float, optional) – Opacity to plot lines with, default 1.0.

  • linewidth (float, optional) – Width of lines, default 1.

mask(x, y, z, ax=0, color='black')[source]

Plot masked out grid points as circles on a single Axes.

Parameters
  • x (array-like of shape (ny, nx) or (nx,)) – The x-coordinates of the grid points.

  • y (array-like of shape (ny, nx) or (ny,)) – The y-coordinates of the grid points.

  • z (masked array of shape (ny, nx) – z-values.

  • ax (int or Matplotlib Axes, optional) – Which Axes to plot on, default 0.

  • color (str, optional) – Circle color, default "black".

save(filename, transparent=False)[source]

Save plots to SVG or PNG file.

Parameters
  • filename (str) – Filename to save to.

  • transparent (bool, optional) – Whether background should be transparent, default False.

save_to_buffer()[source]

Save plots to an io.BytesIO buffer.

Returns

PNG image buffer.

Return type

BytesIO

show()[source]

Show plots in an interactive window, in the usual Matplotlib manner.

title(title, ax=0, color=None)[source]

Set the title of a single Axes.

Parameters
  • title (str) – Title text.

  • ax (int or Matplotlib Axes, optional) – Which Axes to set the title of, default 0.

  • color (str, optional) – Color to set title. May be a string color or the letter "C" followed by an integer in the range "C0" to "C9" to use a color from the tab10 colormap. Default None which is black.

z_values(x, y, z, ax=0, color='green', fmt='.1f', quad_as_tri=False)[source]

Show z values on a single Axes.

Parameters
  • x (array-like of shape (ny, nx) or (nx,)) – The x-coordinates of the grid points.

  • y (array-like of shape (ny, nx) or (ny,)) – The y-coordinates of the grid points.

  • z (array-like of shape (ny, nx) – z-values.

  • ax (int or Matplotlib Axes, optional) – Which Axes to plot on, default 0.

  • color (str, optional) – Color of added text. May be a string color or the letter "C" followed by an integer in the range "C0" to "C9" to use a color from the tab10 colormap. Default "green".

  • fmt (str, optional) – Format to display z-values, default ".1f".

  • quad_as_tri (bool, optional) – Whether to show z-values at the quad_as_tri centers of quads.

Warning

quad_as_tri=True shows z-values for all quads, even if masked.