How to draw lines on an image using matplotlib?


KhoaLearnToCode

I'm having a problem here when trying to draw multiple lines on an image. So, here is my code:

fig=plt.figure()
final = cv2.imread('frame9.jpg')
for p in polar:
    plt.plot([0,1],[p[0],p[0]],c='red')
plt.imshow(final,cmap=plt.cm.gray)
plt.show()

My polar ndarray:

[[ 7.28073704e+01 -1.60300574e-02  3.00000000e+00]
 [ 1.68751118e+02 -2.28065027e-02  4.00000000e+00]
 [ 2.10662349e+02 -3.40033470e-02  6.00000000e+00]
 [ 1.20656915e+02 -1.65935831e-02  5.00000000e+00]
 [ 2.28887705e+01 -8.43417664e-04  5.00000000e+00]
 [ 1.27472877e+01 -7.25424861e-03  2.00000000e+00]
 [ 1.09924214e+02 -1.81133209e-02  3.00000000e+00]
 [ 5.85000000e+01  0.00000000e+00  4.00000000e+00]
 [ 1.57589902e+02 -1.70840018e-02  3.00000000e+00]]

The result just shows the original image without any lines on it.
Is this because of polar ndarray? or something else? Can someone help me.

John C

Since lines are drawn over the image by default, the order in which the draw function is called does not matter here.

To draw something on an image, it is important to know the coordinates of the image. For this there is a parameter which has constraints in the x and y direction. The exact coordinates of your image are not known, so I used some arbitrary values ​​in the example.imshowextent=

imshowForce the aspect ratio to be equal to the range. This way, photos and maps are not distorted. But for calculated values, you usually don't want to use this fixed aspect ratio. You can change it using aspect='auto'.

Also note that the default image draws the origin at the top, as this is standard for most image formats. Sometimes, drawing it on top can need to have origin on the bottom ( origin='lower'VS origin='upper'.

from matplotlib import pyplot as plt
import numpy as np

polar = np.array([[7.28073704e+01, -1.60300574e-02, 3.00000000e+00],
                  [1.68751118e+02, -2.28065027e-02, 4.00000000e+00],
                  [2.10662349e+02, -3.40033470e-02, 6.00000000e+00],
                  [1.20656915e+02, -1.65935831e-02, 5.00000000e+00],
                  [2.28887705e+01, -8.43417664e-04, 5.00000000e+00],
                  [1.27472877e+01, -7.25424861e-03, 2.00000000e+00],
                  [1.09924214e+02, -1.81133209e-02, 3.00000000e+00],
                  [5.85000000e+01, 0.00000000e+00, 4.00000000e+00],
                  [1.57589902e+02, -1.70840018e-02, 3.00000000e+00]])
fig, ax = plt.subplots()
final = 10-np.random.rand(500,500).cumsum(axis=0).cumsum(axis=1)
# final = cv2.imread('frame9.jpg')
plt.imshow(final, cmap=plt.cm.gray, extent=[0, 10, 0,  polar[:,0].max()], aspect='auto', origin='lower')
for p in polar:
    plt.plot([0, 1], [p[0], p[0]], c='red')
plt.show()

Sample

Related


matplotlib: how to draw rectangles on an image

KAI ZHAO : How to draw rectangle on image like this: import matplotlib.pyplot as plt from PIL import Image import numpy as np im = np.array(Image.open('dog.png'), dtype=np.uint8) plt.imshow(im) I don't know what to do next. tmdavison: You can add patches to m

How to draw vertical lines on a given plot in matplotlib?

Francis: Given a graph of a signal in a time representation, how can I draw a line marking the corresponding time index? Specifically, given a signal graph with time indices ranging from 0 to 2.6(s), I want to draw vertical red lines to indicate the correspond

How to draw gradient color lines in matplotlib?

PDRX: To state it in general form, I'm looking for a way to connect multiple points with gradient color lines using matplotlib , but I can't find it anywhere. More specifically, I'm plotting a 2D random walk graph of a single color line. However, since the poi

How to draw lines on an image in OpenCV?

Rahul: If I have polar coordinates of a line, how can I draw it on an image in OpenCV and python? LineThe function takes 2 points, but only draws line segments. I want to draw a line from one edge of an image to the other. Robert Caspary: Just count 2 points.

How to draw gradient color lines in matplotlib?

PDRX: To state it in general form, I'm looking for a way to connect multiple points with gradient color lines using matplotlib , but I can't find it anywhere. More specifically, I'm plotting a 2D random walk graph of a single color line. However, since the poi

How to draw more types of lines in Matplotlib

Timothy There are only 4 linetypes in matplotlib: ['-', '-. ', '-', ':']. Can one make more than 4 different types of linetypes in matplotlib? jakevdp Using dashesparameters to specify custom dash styles, you can create far more than these four types . E.g: im

matplotlib: how to draw rectangles on an image

KAI ZHAO : How to draw rectangle on image like this: import matplotlib.pyplot as plt from PIL import Image import numpy as np im = np.array(Image.open('dog.png'), dtype=np.uint8) plt.imshow(im) I don't know what to do next. tmdavison: You can add patches to m

How to draw transparent lines on an image using PIL?

Retric Here is what I get: draw.line([(x1,y1),(x2,y2)],fill = (255, 255, 255)) The result is a white line, but I want a transparent line, what should I do? Kay Patel As far as I know, the line function takes RGB or RGBA values. try: draw.line([(x1,y1),(x2,y2)

How to draw directed lines in matplotlib?

Wallace In matplotlib, using plt.plot(xs, ys, '-'+marker). This gives you an undirected line, and you can't tell by looking at the resulting plot which end corresponds to the beginning of the array of data points and which corresponds to the end of the array.

How to draw lines on an image in OpenCV?

Rahul: If I have polar coordinates of a line, how can I draw it on an image in OpenCV and python? LineThe function takes 2 points, but only draws line segments. I want to draw a line from one edge of an image to the other. Robert Caspary: Just count 2 points.

How to draw lines on an image using matplotlib?

KhoaLearnToCode I'm having a problem here when trying to draw multiple lines on an image. So, here is my code: fig=plt.figure() final = cv2.imread('frame9.jpg') for p in polar: plt.plot([0,1],[p[0],p[0]],c='red') plt.imshow(final,cmap=plt.cm.gray) plt.show

Draw horizontal lines using subplots Matplotlib

Adrian C I looked at the following thread and it helped, but I wonder if anyone has tried it in a slightly different way. Draw horizontal lines over multiple subplots in python using pyplot Here's what the data looks like so you can test (if necessary) = ctr_d

How to draw transparent lines on an image using PIL?

Retric Here is what I get: draw.line([(x1,y1),(x2,y2)],fill = (255, 255, 255)) The result is a white line, but I want a transparent line, what should I do? Kay Patel As far as I know, the line function takes RGB or RGBA values. try: draw.line([(x1,y1),(x2,y2)

How to draw transparent lines on an image using PIL?

Retric Here is what I get: draw.line([(x1,y1),(x2,y2)],fill = (255, 255, 255)) The result is a white line, but I want a transparent line, what should I do? Kay Patel As far as I know, the line function takes RGB or RGBA values. try: draw.line([(x1,y1),(x2,y2)

How to draw transparent lines on an image using PIL?

Retric Here is what I get: draw.line([(x1,y1),(x2,y2)],fill = (255, 255, 255)) The result is a white line, but I want a transparent line, what should I do? Kay Patel As far as I know, the line function takes RGB or RGBA values. try: draw.line([(x1,y1),(x2,y2)

How to draw lines on an image using matplotlib?

KhoaLearnToCode I'm having a problem here when trying to draw multiple lines on an image. So, here is my code: fig=plt.figure() final = cv2.imread('frame9.jpg') for p in polar: plt.plot([0,1],[p[0],p[0]],c='red') plt.imshow(final,cmap=plt.cm.gray) plt.show

How to draw dashed lines using Matplotlib's ConnectionPatch?

second How can I change the line style of the line drawn by ConnectionPatch? The best general line styles are ":", "-" or "-". The ConnectionPatch code I'm using is: con = ConnectionPatch(color=seas_col[iseas],linewidth=2, xyA=(mean_vals_1[isn,iseas],0),

matplotlib: how to draw rectangles on an image

KAI ZHAO : How to draw rectangle on image like this: import matplotlib.pyplot as plt from PIL import Image import numpy as np im = np.array(Image.open('dog.png'), dtype=np.uint8) plt.imshow(im) I don't know what to do next. tmdavison: You can add patches to m

How to draw lines on an image in OpenCV?

Rahul: If I have polar coordinates of a line, how can I draw it on an image in OpenCV and python? LineThe function takes 2 points, but only draws line segments. I want to draw a line from one edge of an image to the other. Robert Caspary: Just count 2 points.

How to draw lines on an image using matplotlib?

KhoaLearnToCode I'm having a problem here when trying to draw multiple lines on an image. So, here is my code: fig=plt.figure() final = cv2.imread('frame9.jpg') for p in polar: plt.plot([0,1],[p[0],p[0]],c='red') plt.imshow(final,cmap=plt.cm.gray) plt.show

How to draw transparent lines on an image using PIL?

Retric Here is what I get: draw.line([(x1,y1),(x2,y2)],fill = (255, 255, 255)) The result is a white line, but I want a transparent line, what should I do? Kay Patel As far as I know, the line function takes RGB or RGBA values. try: draw.line([(x1,y1),(x2,y2)

How to draw transparent lines on an image using PIL?

Retric Here is what I get: draw.line([(x1,y1),(x2,y2)],fill = (255, 255, 255)) The result is a white line, but I want a transparent line, what should I do? Kay Patel As far as I know, the line function takes RGB or RGBA values. try: draw.line([(x1,y1),(x2,y2)

How to draw lines on an image using matplotlib?

KhoaLearnToCode I'm having a problem here when trying to draw multiple lines on an image. So, here is my code: fig=plt.figure() final = cv2.imread('frame9.jpg') for p in polar: plt.plot([0,1],[p[0],p[0]],c='red') plt.imshow(final,cmap=plt.cm.gray) plt.show

How to draw lines on an image using matplotlib?

KhoaLearnToCode I'm having a problem here when trying to draw multiple lines on an image. So, here is my code: fig=plt.figure() final = cv2.imread('frame9.jpg') for p in polar: plt.plot([0,1],[p[0],p[0]],c='red') plt.imshow(final,cmap=plt.cm.gray) plt.show