Tracer un point ou une droite sur une image avec matplotlib

Exemples de comment tracer un point ou une droite sur une image avec matplotlib.

Tracer un point au dessus d'imshow avec matplotlib

Objectif: tracer un point pour indiquer un minimum global:

'Tracer un point ou une droite sur une image avec matplotlib ' 'Tracer un point ou une droite sur une image avec matplotlib '
'Tracer un point ou une droite sur une image avec matplotlib '

import numpy as np
import matplotlib.pyplot as plt

def f(x,y):
    return (x+y)*np.exp(-5.0*(x**2+y**2))

x,y = np.mgrid[-1:1:100j, -1:1:100j]

z = f(x,y)

plt.imshow(z,origin='lower')
plt.colorbar()

i,j = np.unravel_index(z.argmin(), z.shape)

plt.scatter(i,j,color='r')

plt.xlim(0,100)
plt.ylim(0,100)
plt.title('Draw a point on an image with matplotlib (2/2)')

plt.savefig("draw_on_image_01.png")
plt.show()
plt.close()

Superposer un point et imshow avec l'option extent

Exemple de comment superposer un point et imshow avec matplotlib

'Tracer un point ou une droite sur une image avec matplotlib '
'Tracer un point ou une droite sur une image avec matplotlib '

import numpy as np
import matplotlib.pyplot as plt

def f(x,y):
    return (x+y)*np.exp(-5.0*(x**2+y**2))

x,y = np.mgrid[-1:1:100j, -1:1:100j]

z = f(x,y)

plt.imshow(z,extent=[-1,1,-1,1],origin='lower')
plt.colorbar()

i,j = np.unravel_index(z.argmin(), z.shape)

x = ( i * 2.0 ) / 100.0 - 1.0
y = ( j * 2.0 ) / 100.0 - 1.0

plt.scatter(x,y,color='r')

plt.xlim(-1.0,1.0)
plt.ylim(-1.0,1.0)
plt.title('Draw a point on an image with matplotlib \n (case 2 with extent)')

plt.savefig("draw_on_image_03.png")
plt.show()

Superposer des droites avec imshow

Exemple de comment superposer une droite et imshow avec matplotlib

'Tracer un point ou une droite sur une image avec matplotlib '
'Tracer un point ou une droite sur une image avec matplotlib '

import numpy as np
import matplotlib.pyplot as plt

def f(x,y):
    return (x+y)*np.exp(-5.0*(x**2+y**2))

x,y = np.mgrid[-1:1:100j, -1:1:100j]

z = f(x,y)

plt.imshow(z,extent=[-1,1,-1,1],origin='lower')
plt.colorbar()

i,j = np.unravel_index(z.argmin(), z.shape)

x = ( i * 2.0 ) / 100.0 - 1.0
y = ( j * 2.0 ) / 100.0 - 1.0

plt.axvline(x=x,color='red')
plt.axhline(y=y,color='red')

plt.xlim(-1.0,1.0)
plt.ylim(-1.0,1.0)
plt.title('Draw a line on an image with matplotlib')

plt.savefig("draw_on_image_04.png")
plt.show()

Références

Image

of