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.
- multi_filled(multi_filled, fill_type, ax=0, color=None, **kwargs)[source]#
Plot multiple sets of filled contours on a single axes.
- Parameters:
multi_filled (list of filled contour arrays) – Multiple filled contour sets as returned by
ContourGenerator.multi_filled()
.fill_type (FillType or str) – Type of filled data as returned by
fill_type
, or string equivalent.ax (int or Renderer-specific axes or figure object, optional) – Which axes to plot on, default
0
.color (str or None, optional) – If a string color then this same color is used for all filled contours. If
None
, the default, then the filled contour sets use colors from thetab10
colormap in order, wrapping around to the beginning if more than 10 sets of filled contours are rendered.kwargs – All other keyword argument are passed on to
Renderer.filled()
unchanged.
- multi_lines(multi_lines, line_type, ax=0, color=None, **kwargs)[source]#
Plot multiple sets of contour lines on a single axes.
- Parameters:
multi_lines (list of contour line arrays) – Multiple contour line sets as returned by
ContourGenerator.multi_lines()
.line_type (LineType or str) – Type of line data as returned by
line_type
, or string equivalent.ax (int or Renderer-specific axes or figure object, optional) – Which axes to plot on, default
0
.color (str or None, optional) – If a string color then this same color is used for all lines. If
None
, the default, then the line sets use colors from thetab10
colormap in order, wrapping around to the beginning if more than 10 sets of lines are rendered.kwargs – All other keyword argument are passed on to
Renderer.lines()
unchanged.
- 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
filled()
data 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 theCategory10
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, 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, 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
lines()
data 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 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, 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=True
must have been passed to the constructor.
- save_to_buffer(*, webdriver=None)[source]#
Save plots to an
io.BytesIO
buffer.- 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 theCategory10
palette. DefaultNone
which isblack
.
- 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 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=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. 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
filled()
data 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 thetab10
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, 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, 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
lines()
data 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 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, 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
- 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 thetab10
colormap. Default isNone
which 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
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.