fbench.viz#

Module Contents#

Classes#

FunctionPlotter

Plot a scalar-valued function with an 1-vector or 2-vector input.

VizConfig

Visualization configurations.

Functions#

create_contour_plot(coord, /, *, kws_contourf=None, kws_contour=None, ax=None)

Create a contour plot from X, Y, Z coordinate matrices.

create_coordinates2d(func, x_coord, /)

Create (x, y) pairs from coordinate vector and function.

create_coordinates3d(func, x_coord, y_coord=None, /)

Create X, Y, Z coordinate matrices from coordinate vectors and function.

create_discrete_cmap(n, /, *, name='viridis_r', lower_bound=0.05, upper_bound=0.9)

Create discrete values from colormap.

create_line_plot(coord, /, *, kws_plot=None, ax=None)

Create a line plot from (x, y) pairs.

create_surface_plot(coord, /, *, kws_surface=None, kws_contourf=None, ax=None)

Create a surface plot from X, Y, Z coordinate matrices.

get_1d_plotter()

Get FunctionPlotter instances for functions with 1-vector input.

get_2d_plotter()

Get FunctionPlotter instances for functions with 2-vector input.

plot_optima(optima, /, *, ax=None, ax3d=None, kws_scatter=None)

Add optima as scatter points to plot.

class fbench.viz.FunctionPlotter(func, bounds, with_surface=True, with_contour=True, with_optima=True, n_grid_points=101, x_coord=None, y_coord=None, optima=None, kws_surface=None, kws_contourf=None, kws_contour=None, kws_plot=None, kws_scatter=None)[source]#

Plot a scalar-valued function with an 1-vector or 2-vector input.

Parameters
  • func (callable) – The function to plot.

  • bounds (sequence) – A sequence of (min, max) pairs for each element of the vector.

  • with_surface (bool, default=True) – Specify if the function surface plot should be generated.

  • with_contour (bool, default=True) – Specify if the contour plot should be generated.

  • with_optima (bool, default=True) – Specify if scatter points for the optima should be added.

  • n_grid_points (int, default=101) – Specify the number of grid points on one axis. Ignored if x_coord or y_coord is specified.

  • x_coord (sequence, default=None) – Specify coordinates on the x-axis.

  • x_coord – Specify coordinates on the y-axis.

  • optima (sequence[Optimum], default=None) – Specify optima to plot. If None, retrieve them from get_optima(). Note that optima are only added to the plot if a defintion exists.

  • kws_surface (dict of keyword arguments, default=None) – The kwargs are passed to mpl_toolkits.mplot3d.axes3d.Axes3D.plot_surface. By default, using configuration: VizConfig.get_kws_surface__YlOrBr_r(). Optionally specify a dict of keyword arguments to update configurations.

  • kws_contourf (dict of keyword arguments, default=None) – The kwargs are passed to mpl_toolkits.mplot3d.axes3d.Axes3D.contourf. By default, using configuration: VizConfig.get_kws_contourf__YlOrBr_r(). Optionally specify a dict of keyword arguments to update configurations.

  • kws_contour (dict of keyword arguments, default=None) – The kwargs are passed to matplotlib.axes.Axes.contour. By default, using configuration: VizConfig.get_kws_contour__base(). Optionally specify a dict of keyword arguments to update configurations.

  • kws_plot (dict of keyword arguments, default=None) – The kwargs are passed to matplotlib.axes.Axes.plot. By default, using configuration: VizConfig.get_kws_plot__base(). Optionally specify a dict of keyword arguments to update configurations.

  • kws_scatter (dict of keyword arguments, default=None) – The kwargs are passed to matplotlib.axes.Axes.scatter or mpl_toolkits.mplot3d.axes3d.Axes3D.scatter. By default, using configuration: VizConfig.get_kws_scatter__base(). Optionally specify a dict of keyword arguments to update configurations.

Notes

See also

fbench.get_optima

Retrieve optima for defined functions.

Examples

>>> import fbench
>>> fbench.viz.FunctionPlotter(func=fbench.sphere, bounds=[(-5, 5)])
FunctionPlotter(func=sphere, bounds=[(-5, 5)])
property bounds(self)#

Bounds to use for the plot.

property func(self)#

The function to plot.

plot(self, fig=None, ax=None, ax3d=None)[source]#

Generate the plot.

Parameters
  • fig (matplotlib.figure.Figure, default=None) – Optionally supply a Figure object. If None, the current Figure object is retrieved.

  • ax (matplotlib.axes.Axes, default=None) – Optionally supply an Axes object. If None, the current Axes object is retrieved.

  • ax3d (mpl_toolkits.mplot3d.axes3d.Axes3D, default=None) – Optionally supply an Axes3D object. If None, the current Axes3D object is retrieved.

Returns

  • fig (matplotlib.figure.Figure) – The Figure object.

  • ax (matplotlib.axes.Axes) – The Axes object.

  • ax3d (mpl_toolkits.mplot3d.axes3d.Axes3D) – The Axes3D object of the surface.

Notes

When creating both a surface and contour plot and either ax or ax3d is specified, it is best to also supply fig. To this end, it might be easier to only supply a fig object.

class fbench.viz.VizConfig[source]#

Bases: enum.Enum

Visualization configurations.

classmethod get_kws_contour__base(cls)[source]#

Returns kwargs for .contour(): base configuration.

classmethod get_kws_contourf__YlOrBr(cls)[source]#

Returns kwargs for .contourf(): YlOrBr configuration for dark max.

classmethod get_kws_contourf__YlOrBr_r(cls)[source]#

Returns kwargs for .contourf(): YlOrBr_r configuration for dark min.

classmethod get_kws_contourf__base(cls)[source]#

Returns kwargs for .contourf(): base configuration.

classmethod get_kws_plot__base(cls)[source]#

Returns kwargs for .plot(): base configuration.

classmethod get_kws_scatter__base(cls)[source]#

Returns kwargs for .scatter(): base configuration.

classmethod get_kws_surface__YlOrBr(cls)[source]#

Returns kwargs for .plot_surface(): YlOrBr configuration for dark max.

classmethod get_kws_surface__YlOrBr_r(cls)[source]#

Returns kwargs for .plot_surface(): YlOrBr_r configuration for dark min.

classmethod get_kws_surface__base(cls)[source]#

Returns kwargs for .plot_surface(): base configuration.

fbench.viz.create_contour_plot(coord, /, *, kws_contourf=None, kws_contour=None, ax=None)[source]#

Create a contour plot from X, Y, Z coordinate matrices.

Parameters
  • coord (CoordinateMatrices) – The X, Y, Z coordinate matrices to plot.

  • kws_contourf (dict of keyword arguments, default=None) – The kwargs are passed to matplotlib.axes.Axes.contourf. By default, using configuration: VizConfig.get_kws_contourf__YlOrBr_r(). Optionally specify a dict of keyword arguments to update configurations.

  • kws_contour (dict of keyword arguments, default=None) – The kwargs are passed to matplotlib.axes.Axes.contour. By default, using configuration: VizConfig.get_kws_contour__base(). Optionally specify a dict of keyword arguments to update configurations.

  • ax (matplotlib.axes.Axes, default=None) – Optionally supply an Axes object. If None, the current Axes object is retrieved.

Returns

ax – The Axes object with filled contours and superimposed contour lines.

Return type

matplotlib.axes.Axes

Notes

fbench.viz.create_coordinates2d(func, x_coord, /)[source]#

Create (x, y) pairs from coordinate vector and function.

For each value of \(x\), compute function value \(y = f(x)\).

Parameters
  • func (Callable[[np.ndarray], float]) – A scalar-valued function that takes an 1-vector as input.

  • x_coord (array_like) – An one-dimensional array for the x-coordinates of the grid.

Returns

The (x, y) coordinate pairs.

Return type

CoordinatePairs

Notes

Function is curried.

Examples

>>> import fbench
>>> fbench.viz.create_coordinates2d(fbench.sphere, [-2, -1, 0, 1, 2])
CoordinatePairs(x=array([-2, -1,  0,  1,  2]), y=array([4., 1., 0., 1., 4.]))
fbench.viz.create_coordinates3d(func, x_coord, y_coord=None, /)[source]#

Create X, Y, Z coordinate matrices from coordinate vectors and function.

First, a meshgrid of (x, y)-coordinates is constructed from the coordinate vectors. Then, the z-coordinate for each (x, y)-point is computed using the function.

Parameters
  • func (Callable[[np.ndarray], float]) – A scalar-valued function that takes a two-dimensional, real vector as input.

  • x_coord (array_like) – An one-dimensional array for the x-coordinates of the grid.

  • y_coord (array_like, default=None) – An one-dimensional array for the y-coordinates of the grid. If None, y_coord equals x_coord.

Returns

The coordinate matrices.

Return type

CoordinateMatrices

Notes

Function is curried.

Examples

>>> import fbench
>>> fbench.viz.create_coordinates3d(fbench.sphere, [-1, 0, 1])
CoordinateMatrices(x=array([[-1,  0,  1],
       [-1,  0,  1],
       [-1,  0,  1]]), y=array([[-1, -1, -1],
       [ 0,  0,  0],
       [ 1,  1,  1]]), z=array([[2., 1., 2.],
       [1., 0., 1.],
       [2., 1., 2.]]))
fbench.viz.create_discrete_cmap(n, /, *, name='viridis_r', lower_bound=0.05, upper_bound=0.9)[source]#

Create discrete values from colormap.

Parameters
  • n (int) – Specify the number of discrete values.

  • name (str, default="viridis_r") – Specify the name of the colormap.

  • lower_bound (float, default=0.05,) – Specify the lower bound of the colormap.

  • upper_bound (float, default=0.9,) – Specify the upper bound of the colormap.

Returns

Discrete values from colormap.

Return type

list[tuple[float, float, float, float]]

Notes

Function is curried.

Examples

>>> import fbench
>>> fbench.viz.create_discrete_cmap(2)
[(0.876168, 0.891125, 0.09525, 1.0), (0.282623, 0.140926, 0.457517, 1.0)]
fbench.viz.create_line_plot(coord, /, *, kws_plot=None, ax=None)[source]#

Create a line plot from (x, y) pairs.

Parameters
  • coord (CoordinatePairs) – The (x, y) coordinate pairs.

  • kws_plot (dict of keyword arguments, default=None) – The kwargs are passed to matplotlib.axes.Axes.plot. By default, using configuration: VizConfig.get_kws_plot__base(). Optionally specify a dict of keyword arguments to update configurations.

  • ax (matplotlib.axes.Axes, default=None) – Optionally supply an Axes object. If None, the current Axes object is retrieved.

Returns

ax – The Axes object.

Return type

matplotlib.axes.Axes

Notes

fbench.viz.create_surface_plot(coord, /, *, kws_surface=None, kws_contourf=None, ax=None)[source]#

Create a surface plot from X, Y, Z coordinate matrices.

Parameters
  • coord (CoordinateMatrices) – The X, Y, Z coordinate matrices to plot.

  • kws_surface (dict of keyword arguments, default=None) – The kwargs are passed to mpl_toolkits.mplot3d.axes3d.Axes3D.plot_surface. By default, using configuration: VizConfig.get_kws_surface__YlOrBr_r(). Optionally specify a dict of keyword arguments to update configurations.

  • kws_contourf (dict of keyword arguments, default=None) – The kwargs are passed to mpl_toolkits.mplot3d.axes3d.Axes3D.contourf. By default, using configuration: VizConfig.get_kws_contourf__YlOrBr_r(). Optionally specify a dict of keyword arguments to update configurations.

  • ax (mpl_toolkits.mplot3d.axes3d.Axes3D, default=None) – Optionally supply an Axes3D object. If None, the current Axes3D object is retrieved.

Returns

ax – The Axes3D object of the surface.

Return type

mpl_toolkits.mplot3d.axes3d.Axes3D

Notes

fbench.viz.get_1d_plotter()[source]#

Get FunctionPlotter instances for functions with 1-vector input.

Returns

Predefined FunctionPlotter instances.

Return type

dict[str, FunctionPlotter]

fbench.viz.get_2d_plotter()[source]#

Get FunctionPlotter instances for functions with 2-vector input.

Returns

Predefined FunctionPlotter instances.

Return type

dict[str, FunctionPlotter]

fbench.viz.plot_optima(optima, /, *, ax=None, ax3d=None, kws_scatter=None)[source]#

Add optima as scatter points to plot.

Parameters
  • optima (sequence of Optimum) – The optima to plot.

  • ax (matplotlib.axes.Axes, default=None) – Specify the Axes object if scatter points should be added to it.

  • ax3d (mpl_toolkits.mplot3d.axes3d.Axes3D, default=None) – Specify the Axes3D object if scatter points should be added to it.

  • kws_scatter (dict of keyword arguments, default=None) – The kwargs are passed to matplotlib.axes.Axes.scatter or mpl_toolkits.mplot3d.axes3d.Axes3D.scatter. By default, using configuration: VizConfig.get_kws_scatter__base(). Optionally specify a dict of keyword arguments to update configurations.

Returns

  • ax (matplotlib.axes.Axes) – The Axes object.

  • ax3d (mpl_toolkits.mplot3d.axes3d.Axes3D) – The Axes3D object of the surface.