I am familiar with Mathematica to a certain extent, but its subtleties still elude me.

Currently I am trying to solve the following problem:
The function $f(x,y)$ is continuous. I know how to create a contourplot of it. I also have a list of points {{x,y}, ...} which I would like to insert into my contour plot.

I am working on some genetic algorithms and would like to visualize how the population converges to the global optimum. This is why I would like to show the population (or at least the best candidate of each generation) in the contour plot.

How could this be done?

  • 2
    $\begingroup$ A contour plot is 2D. How do you want to include 3D points in it? $\endgroup$ – Dr. belisarius Mar 29 '13 at 11:26
  • $\begingroup$ contour plot projects onto the x-y plane the levels of a function in two variables. It is 2D as opposed to your triples. You could project them too using ListContourPlot or use a the contour plot as a texture for the x-y plane (see related) $\endgroup$ – gpap Mar 29 '13 at 11:31
  • $\begingroup$ i did not mean to use 3D data - my question was perhaps somewhat confusion. Just wanted to plot the population - only (x,y) values. I will adjust the phrasing accordingly $\endgroup$ – Probabilitnator Mar 29 '13 at 11:56

I guess one should use the following plot to visualize convergence. For optimization consider this example, and collect the points during NMinimize evaluation.

f[x_, y_] := 20 Sin[\[Pi]/2 (x - 2 \[Pi])] +
20 Sin[\[Pi]/2 (y - 2 \[Pi])] + (x - 2 \[Pi])^2 + (y - 2 \[Pi])^2;
pts = Reap[sol = NMinimize[f[x, y], {x, y}, 
  Method -> {"SimulatedAnnealing", "PerturbationScale" -> 3, 
    "BoltzmannExponent" -> 
     Function[{i, df, f0}, -df/(Exp[i/10])]}, 
  StepMonitor :> (Sow[{x, y}])];];

Let's visualize it. Here blue is the starting point and red is the global optimum.

ContourPlot[f[x, y], {x, -2, 7}, {y, -2, 7}, Contours -> 10, 
ColorFunction -> "BlueGreenYellow", PlotLegends -> Automatic, 
Epilog -> ({Red, PointSize[.01], Arrowheads[0.025], 
Arrow /@ Partition[pts[[2, 1]], 2, 1], Yellow, 
Point /@ pts[[2, 1]], Blue, PointSize[.02], Point[pts[[2, 1, 1]]],
 Red, PointSize[.02], Point[{x, y} /. sol[[2]]]}),ContourLabels -> True]

enter image description here


For plotting 3D points on a 2D surface, check out this wonderful post by Rahul Narain.


  • $\begingroup$ this is beautiful. Problem is I wrote my own genetic algorithm in C++ and output the different evolutionary steps into a CSV-file. This is why I need to plot a list of data. $\endgroup$ – Probabilitnator Mar 29 '13 at 11:59
  • $\begingroup$ still i had no idea sth like that wos doable. Beautiful :) $\endgroup$ – Probabilitnator Mar 29 '13 at 12:03
  • $\begingroup$ @Probabilitnator please note pts are just the points visited by the algorithm. So what you need to do is import the points of your genetic steps into Mathematica. CSV import is easy. $\endgroup$ – PlatoManiac Mar 29 '13 at 12:05
  • $\begingroup$ now i see. Thanks now this is even more beautiful <3 ! $\endgroup$ – Probabilitnator Mar 29 '13 at 12:13
  • 1
    $\begingroup$ @chris mostly copied from docs :( Thx though.... $\endgroup$ – PlatoManiac Mar 29 '13 at 21:20

Since presumably f[x,y] is the same for the GA result as the contour plot, you can display the points at which your GA has been evaluated on the contour plot in this way:

gaPoints= Tuples[Range@12, 2];
f[{x_, y_}] := {Sin@x, Cos@y}

g1 = ListPlot[gaPoints];
g2 = ContourPlot[f[{x, y}], {x, 0, 4 Pi}, {y, 0, 4 Pi}];

And plot the two graphs as:

Show[g2, g1]

Mathematica graphics

  • $\begingroup$ so basically I am just overlapping the two plots ?? - this is easier than I would have thought and exactly what I am looking for. Could you perhaps explain some of the syntax ? (why use 'Sin@x' ) $\endgroup$ – Probabilitnator Mar 29 '13 at 12:02
  • $\begingroup$ Yes indeed you can combine plots by simply overlapping them :) Sin@x is just a short form of Sin[x]. $\endgroup$ – image_doctor Mar 29 '13 at 15:00
  • $\begingroup$ how can I get rid of that grid ? $\endgroup$ – Probabilitnator Mar 29 '13 at 16:39
  • 1
    $\begingroup$ You can use the ContourLines Options, ContourPlot[f[{x, y}], {x, 0, 4 Pi}, {y, 0, 4 Pi}, ContourLines -> False] $\endgroup$ – image_doctor Mar 29 '13 at 23:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.