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: tuple[int, int], seed: int = 2187, mask_fraction: float = 0.0) tuple[CoordinateArray, CoordinateArray, CoordinateArray | np.ma.MaskedArray[Any, Any]] [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 ifmask_fraction
is greater than zero.- Return type:
Tuple of 3 arrays
- contourpy.util.data.simple(shape: tuple[int, int], want_mask: bool = False) tuple[CoordinateArray, CoordinateArray, CoordinateArray | np.ma.MaskedArray[Any, Any]] [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 ifwant_mask=True
.- Return type:
Tuple of 3 arrays
- class contourpy.util.renderer.Renderer[source]#
Abstract base class for renderers, defining the interface that they must implement.
- class contourpy.util.bokeh_renderer.BokehRenderer(nrows: int = 1, ncols: int = 1, figsize: tuple[int, int] = (9, 9), show_frame: bool = True, want_svg: bool = False)[source]#
- filled(filled: FillReturn, fill_type: FillType, ax: figure | int = 0, color: str = 'C0', alpha: float = 0.7) None [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 byfill_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 theCategory10
palette. Default"C0"
.alpha (float, optional) – Opacity to plot with, default
0.7
.
- grid(x: ArrayLike, y: ArrayLike, ax: figure | int = 0, color: str = 'black', alpha: float = 0.1, point_color: str | None = None, quad_as_tri_alpha: float = 0) None [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, defaultNone
.quad_as_tri_alpha (float, optional) – Opacity to plot
quad_as_tri
grid, default0
.
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 theCategory10
palette.Warning
quad_as_tri_alpha > 0
plots all quads as though they are unmasked.
- lines(lines: LineReturn, line_type: LineType, ax: figure | int = 0, color: str = 'C0', alpha: float = 1.0, linewidth: float = 1) None [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 byline_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 theCategory10
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: ArrayLike, y: ArrayLike, z: ArrayLike | np.ma.MaskedArray[Any, Any], ax: figure | int = 0, color: str = 'black') None [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: str, transparent: bool = False) None [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() BytesIO [source]#
Save plots to an
io.BytesIO
buffer.- Returns:
PNG image buffer.
- Return type:
BytesIO
- title(title: str, ax: figure | int = 0, color: str | None = None) 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 theCategory10
palette. DefaultNone
which isblack
.
- z_values(x: ArrayLike, y: ArrayLike, z: ArrayLike, ax: figure | int = 0, color: str = 'green', fmt: str = '.1f', quad_as_tri: bool = False) None [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 theCategory10
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: int = 1, ncols: int = 1, figsize: tuple[int, int] = (9, 9), show_frame: bool = True, backend: str | None = None, gridspec_kw: dict[str, Any] | None = None)[source]#
- filled(filled: cpy.FillReturn, fill_type: FillType, ax: Axes | int = 0, color: str = 'C0', alpha: float = 0.7) None [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 byfill_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 thetab10
colormap. Default"C0"
.alpha (float, optional) – Opacity to plot with, default
0.7
.
- grid(x: ArrayLike, y: ArrayLike, ax: Axes | int = 0, color: str = 'black', alpha: float = 0.1, point_color: str | None = None, quad_as_tri_alpha: float = 0) None [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, defaultNone
.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 thetab10
colormap.Warning
quad_as_tri_alpha > 0
plots all quads as though they are unmasked.
- lines(lines: cpy.LineReturn, line_type: LineType, ax: Axes | int = 0, color: str = 'C0', alpha: float = 1.0, linewidth: float = 1) None [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 byline_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 thetab10
colormap. Default"C0"
.alpha (float, optional) – Opacity to plot lines with, default
1.0
.linewidth (float, optional) – Width of lines, default
1
.
- mask(x: ArrayLike, y: ArrayLike, z: ArrayLike | np.ma.MaskedArray[Any, Any], ax: Axes | int = 0, color: str = 'black') None [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: str, transparent: bool = False) None [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() BytesIO [source]#
Save plots to an
io.BytesIO
buffer.- Returns:
PNG image buffer.
- Return type:
BytesIO
- title(title: str, ax: Axes | int = 0, color: str | None = None) 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 thetab10
colormap. Default isNone
which uses Matplotlib’s default title color that depends on the stylesheet in use.
- z_values(x: ArrayLike, y: ArrayLike, z: ArrayLike, ax: Axes | int = 0, color: str = 'green', fmt: str = '.1f', quad_as_tri: bool = False) None [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 thetab10
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.