Search code examples
pythonpython-3.xmatplotlibplotgraphing

Plotting convergence of points on top of a contour plot in Python


I have made a contour plot in Python for an optimization algorithm. Now, I am having trouble plotting the iterative points in Python. My (x,y) points are stored as vectors within an array. Here is an example of such:

[array([-1, -2]), array([ 0.93396226, -0.45283019]), array([2.86792453, 1.09433962]),...]

How could I plot each of these points on top of my existing contour plot using matplotlib?

Further explanation: I have this array of arrays. They represent x- and y-coordinates. I want to put these (x,y) coordinates on top of my contour plot.


Solution

  • I think, you are looking for something like that:

    import pylab as plt
    
    # ....
    
    a = [array([-1, -2]),
     array([ 0.93396226, -0.45283019]),
     array([2.86792453, 1.09433962]),...]
    x = array(a)[:, 0]
    y = array(a)[:, 1]
    
    # some contourf stuff
    
    plt.scatter(x, y)