Example usage#

Version check#

import matplotlib.pyplot as plt
import numpy as np

import fbench
from fbench.viz import FunctionPlotter, VizConfig

print(fbench.__version__)
1.0.1

FunctionPlotter#

Functions with 1-vector input#

func = lambda x: (x**2).sum()
bounds = [(-5, 5)]
default = FunctionPlotter(func, bounds)
default.plot()
plt.show()
_images/290e206059944aa767ef54b8d6ef204a8936a7764ab49f81fe2a7d01dc53a515.png
less_grid_points = FunctionPlotter(func, bounds, n_grid_points=7)
less_grid_points.plot()
plt.show()
_images/4d957156ddf416e042eb46dc9991c6c5185ad3b9d877ea684012206b8c29dc51.png
specific_grid_points = FunctionPlotter(
    func,
    bounds,
    x_coord=[-1, 0, 1, 2, 3, 4],
)
specific_grid_points.plot()
plt.show()
_images/da194813111a0080e26c550a08b007017847d92341dcc430ec7eb0123bcc9372.png
kws_plot = VizConfig.get_kws_plot__base()

kws_plot.update({"color": "black", "linewidth": 3.14159})

custom_kws = FunctionPlotter(
    func,
    bounds,
    kws_plot=kws_plot,
)

_, ax = plt.subplots(1, 1, figsize=(8, 6))

_, ax, _ = custom_kws.plot(ax=ax)

ax.set_xlabel(r"$ \mathbf{x} $")
ax.set_ylabel(r"$ f(\mathbf{x}) $", rotation=0, labelpad=15)
ax.set_title(r"$ f(\mathbf{x}) = x_{1}^{2} $")

plt.show()
_images/57290fb4020734948a38142cf12feaa04b707e80f7d36124c71ec44eaf4bff94.png

Functions with 2-vector input#

func = lambda x: (x**2).sum()
bounds = [(-5, 5)] * 2
default = FunctionPlotter(func, bounds)
default.plot()
plt.show()
_images/986ab807121151adf8ec5602bef496ad1b84a2c678126bf674cce4d46d90ce27.png
only_surface = FunctionPlotter(func, bounds, with_contour=False)
only_surface.plot()
plt.show()
_images/bf919ef9c43cc3f1c49dac3ae9d78ee982bfb567ebb0e88b64fe256c423739da.png
only_contour = FunctionPlotter(func, bounds, with_surface=False)
only_contour.plot()
plt.show()
_images/bf7c03492f6d31f65e8027f13c578dbf8c1739fc790e3170ba4922f4567e9e6c.png
less_grid_points = FunctionPlotter(func, bounds, n_grid_points=10)
less_grid_points.plot()
plt.show()
_images/0aa36d65ea6ac0c554addf9354c20a56379a86093763f1767862e584e5990fb3.png
specific_grid_points = FunctionPlotter(
    func,
    bounds,
    x_coord=[-2, -1, 0, 1, 2],
    y_coord=[-1, -0.5, 0, 0.5, 1],
)
specific_grid_points.plot()
plt.show()
_images/3202a2155356b3ac651d30e63ab3103bc12e584c01b2de59a39662a1a5f4ae3c.png
plt.colormaps()[::2][:5]
['magma', 'plasma', 'cividis', 'twilight_shifted', 'Blues']
kws_surface = VizConfig.get_kws_surface__base()
kws_contourf = VizConfig.get_kws_contourf__base()
kws_contour = VizConfig.get_kws_contour__base()

kws_surface.update({"cmap": plt.get_cmap("cividis_r")})
kws_contourf.update({"cmap": plt.get_cmap("cividis_r")})
kws_contour.update({"colors": "black", "alpha": 0.4, "levels": 6, "linewidths": 0.414})

custom_kws = FunctionPlotter(
    func,
    bounds,
    kws_surface=kws_surface,
    kws_contourf=kws_contourf,
    kws_contour=kws_contour,
)

fig = plt.figure(figsize=(9, 3))

_, ax, ax3d = custom_kws.plot(fig=fig)

ax.set_xlabel(r"$ x_{1} $")
ax.set_ylabel(r"$ x_{2} $", rotation=0)
ax.set_title(r"$ f(\mathbf{x}) = \sum_{i=1}^{2} x_i^2 $")

ax3d.set_xlabel(r"$ x_{1} $")
ax3d.set_ylabel(r"$ x_{2} $")
ax3d.set_zlabel(r"$ f(\mathbf{x}) $")

plt.show()
_images/0dad7c8ae980348fba316d652d8a1583d5f752291df3b57ca3e3dc6aba49aa2d.png
default_with_predefined_func = FunctionPlotter(
    func=fbench.sphere,
    bounds=((-2, 2), (-2, 2)),
)
default_with_predefined_func.plot()
plt.show()
_images/147161207292187d592a36b90d1a1443b7fc082eb21b569d3a102e3a9baa3d80.png
lambda_func_with_manual_optimum = FunctionPlotter(
    func=lambda x: -fbench.sphere(x) + 4,
    bounds=((-3, 3), (-3, 3)),
    with_optima=True,
    optima=[fbench.structure.Optimum(np.zeros(2), 4)],
)
lambda_func_with_manual_optimum.plot()
plt.show()
_images/ce3f9ec2ad0e3dbb801827606007e34bb3315869438b7fafa5149d3555c03a6d.png
fig = plt.figure(figsize=(24, 10))

kws_surface = dict(cmap=plt.get_cmap("cividis_r"), linewidth=0)
kws_contourf = dict(cmap=plt.get_cmap("cividis_r"))
kws_contour = dict(linewidths=0)

rotated_surface = FunctionPlotter(
    func=lambda x: -fbench.sphere(x) + 4,
    bounds=((-3, 3), (-3, 3)),
    n_grid_points=303,
    with_optima=True,
    optima=[fbench.structure.Optimum(np.zeros(2), 4)],
    with_contour=False,
    kws_surface=kws_surface,
    kws_contourf=kws_contourf,
    kws_contour=kws_contour,
)
_, _, ax3d = rotated_surface.plot(fig=fig)

ax3d.set_xlabel(r"$ x_{1} $")
ax3d.set_ylabel(r"$ x_{2} $")
ax3d.set_zlabel(r"$ f(\mathbf{x}) $")

ax3d.view_init(15, -70, 0)
ax3d.set_box_aspect(None, zoom=0.9)

plt.show()
_images/8824f7b77ea037f35bcfb4379ed0a131c5c7eee2e0e783e9170e6d69ccb3a2d6.png