0

I have a python code like this:

import matplotlib.pyplot as plt

import matplotlib.image as mpimg

img = mpimg.imread('xxx.png')

fig = plt.figure(figsize=(8,10))

ax1 = fig.add_axes([0., 0.92, 0.1, 0.08])

ax1.imshow(img)

ax1.axvline(0.5, 0, 1, color='gray')

plt.show()

Running this code shows the img in the axes area. However, the vline does not show up.

If I remove the line: ax1.imshow(img) and run again I do see the vline.

Any suggestion?

WebDevBooster
  • 14,674
  • 9
  • 66
  • 70
hpw
  • 1
  • 1

1 Answers1

1

Your line is probably being drawn over on the left hand y-axis. The x coordinate of ax.axvline is in data coordinates. The x-axis limits from the ax.imshow will be the number of pixels in the x-direction of the image. So, an x coordinate of 0.5 will be in the centre of the left-most pixel of the imshow.

Try increasing the x coordinate of the axvline to a bigger number, and you will probably see it.

From the axvline docs:

x : scalar, optional, default: 0

    x position in data coordinates of the vertical line.

Consider this example:

import matplotlib.pyplot as plt
import matplotlib.image as mpimg

img = mpimg.imread('stinkbug.png')

fig, ax = plt.subplots(1)

ax.imshow(img)

ax.axvline(0.5, 0, 1, color='blue')
ax.axvline(200, 0, 1, color='red')

plt.show()

Note the blue line right against the left hand y-axis.

enter image description here

tmdavison
  • 64,360
  • 12
  • 187
  • 165