Plot over image background in python

I would like to plot a graph against the background of the image using matplotlib. I found how to do this in matlab http://www.peteryu.ca/tutorials/matlab/plot_over_image_background

I tried something like this:

im = plt.imread("dd.png") implot = plt.imshow(im) theta=np.linspace(0,2*np.pi,50) z=np.cos(theta)*39+145 t=np.sin(theta)*39+535-78+39 plt.plot(z,t) plt.show() 

but it gave me something really ugly:

something really ugly

+5
source share
1 answer

As in the MATLAB example you contacted, you must specify the desired image length when invoking imshow .

By default, matplotlib and MATLAB place the top left corner of the image at the origin, go down and to the right from there, and set each pixel as a 1x1 square in the coordinate space. This is what makes your image.

You can control this with the extent parameter, which takes the form of a list [left, right, bottom, top] .

Inability to use is as follows:

 import matplotlib.pyplot as plt img = plt.imread("airlines.jpg") fig, ax = plt.subplots() ax.imshow(img) 

enter image description here

You can see that we have 1600 x 1200 from Samuel L. Jackson, quite frankly, rather annoyed by the snake aboard his airline flight.

But if we want to build a line in size from 0 to 300 in both dimensions above this, we can do just that:

 fig, ax = plt.subplots() x = range(300) ax.imshow(img, extent=[0, 400, 0, 300]) ax.plot(x, x, '--', linewidth=5, color='firebrick') 

enter image description here

I don't know if this line will help Mr. Jackson with his snake problem. At least this will not complicate the situation.

+18
source

Source: https://habr.com/ru/post/1239165/


All Articles