contourpy.util#
This module includes Renderer classes for both Bokeh and Matplotlib to visualise contours
calculated by ContourPy. These renderers are used internally in the test suite and for examples in
this documentation. Please use them to experiment with ContourPy, but it is recommended that you
don’t use them in downstream projects as they are liable to change at short notice without a
formal deprecation period. Instead use Bokeh and Matplotlib directly as they both use
ContourPy for their contour calculations and they provide much richer visualisation functionality.
- 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=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, unlikeMplRenderer, 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 or str) – Type of
filleddata as returned byfill_type, or a string equivalent.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 theCategory10palette. 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
Noneif grid points should not be plotted, defaultNone.quad_as_tri_alpha (float, optional) – Opacity to plot
quad_as_trigrid, 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 theCategory10palette.Warning
quad_as_tri_alpha > 0plots 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 or str) – Type of
linesdata as returned byline_type, or a string equivalent.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 theCategory10palette. 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, *, webdriver=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.webdriver (WebDriver, optional) –
Selenium WebDriver instance to use to create the image.
New in version 1.1.1.
Warning
To output to SVG file,
want_svg=Truemust have been passed to the constructor.
- save_to_buffer(*, webdriver=None)[source]#
Save plots to an
io.BytesIObuffer.- Parameters:
webdriver (WebDriver, optional) –
Selenium WebDriver instance to use to create the image.
New in version 1.1.1.
- Returns:
PNG image buffer.
- Return type:
BytesIO
- 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 theCategory10palette. DefaultNonewhich isblack.
- z_values(x, y, z, ax=0, color='green', fmt='.1f', quad_as_tri=False)[source]#
Show
zvalues 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 theCategory10palette. 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_tricentres of quads.
Warning
quad_as_tri=Trueshows 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
Nonefor default backend. DefaultNone.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 or str) – Type of
filleddata as returned byfill_type, or string equivalentax (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 thetab10colormap. 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
Noneif grid points should not be plotted, defaultNone.quad_as_tri_alpha (float, optional) – Opacity to plot
quad_as_trigrid, 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 thetab10colormap.Warning
quad_as_tri_alpha > 0plots 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 or str) – Type of
linesdata as returned byline_type, or string equivalent.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 thetab10colormap. 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.BytesIObuffer.- Returns:
PNG image buffer.
- Return type:
BytesIO
- 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 thetab10colormap. Default isNonewhich uses Matplotlib’s default title color that depends on the stylesheet in use.
- z_values(x, y, z, ax=0, color='green', fmt='.1f', quad_as_tri=False)[source]#
Show
zvalues 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 thetab10colormap. 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_tricenters of quads.
Warning
quad_as_tri=Trueshows z-values for all quads, even if masked.