Filled Contour Plot of Test Function with Samples
We may want to show the progress of an optimization algorithm to get an idea of its behavior in the context of the shape of the objective function.
In this case, we can simulate the points chosen by an optimization algorithm with random coordinates in the input space.
simulate a sample made by an optimization algorithm
seed(1)
sample_x = r_min + rand(10) * (r_max - r_min)
sample_y = r_min + rand(10) * (r_max - r_min)
These points can then be plotted directly as black circles and their context color can give an idea of their relative quality.
plot the sample as black circles
pyplot.plot(sample_x, sample_y, ‘o’, color=‘black’)
Tying this together, the complete example of a filled contour plot with optimal and input sample plotted is listed below.
filled contour plot for 2d objective function and show the optima and sample
from numpy import arange
from numpy import meshgrid
from numpy.random import seed
from numpy.random import rand
from matplotlib import pyplot
objective function
def objective(x, y):
return x2.0 + y2.0
define range for input
r_min, r_max = -5.0, 5.0
sample input range uniformly at 0.1 increments
xaxis = arange(r_min, r_max, 0.1)
yaxis = arange(r_min, r_max, 0.1)
create a mesh from the axis
x, y = meshgrid(xaxis, yaxis)
compute targets
results = objective(x, y)
simulate a sample made by an optimization algorithm
seed(1)
sample_x = r_min + rand(10) * (r_max - r_min)
sample_y = r_min + rand(10) * (r_max - r_min)
create a filled contour plot with 50 levels and jet color scheme
pyplot.contourf(x, y, results, levels=50, cmap=‘jet’)
define the known function optima
optima_x = [0.0, 0.0]
draw the function optima as a white star
pyplot.plot([optima_x[0]], [optima_x[1]], ‘*’, color=‘white’)
plot the sample as black circles
pyplot.plot(sample_x, sample_y, ‘o’, color=‘black’)
show the plot
pyplot.show()
Running the example, we can see the filled contour plot as before with the optima marked.