I am using matplotlib to draw vertical and horizontal grid lines over an image. The resulting image will have numbered squares over it. To do this, I used this code:
def drawGridOverImage(image):
"""Draw a numbered grid with 1cm2 boxes over an image."""
import matplotlib.pyplot as plt
import matplotlib.ticker as plticker
my_dpi=300.
# Set up figure
fig=plt.figure(figsize=(float(image.size[0])/my_dpi,float(image.size[1])/my_dpi),dpi=my_dpi)
ax=fig.add_subplot(111)
# Remove whitespace from around the image
fig.subplots_adjust(left=0,right=1,bottom=0,top=1)
# Set the gridding interval
# my_dpi is dots per inch, so each square is 1 inch2
myInterval=my_dpi
loc = plticker.MultipleLocator(base=myInterval)
ax.xaxis.set_major_locator(loc)
ax.yaxis.set_major_locator(loc)
# Add the grid
ax.grid(True, which='major', axis='both', linestyle='-')
# Add the image
ax.imshow(image)
# Find number of gridsquares in x and y direction
nx=abs(int(float(ax.get_xlim()[1]-ax.get_xlim()[0])/float(myInterval)))
ny=abs(int(float(ax.get_ylim()[1]-ax.get_ylim()[0])/float(myInterval)))
# Add some labels to the gridsquares
for j in range(ny):
y=myInterval/2+j*myInterval
for i in range(nx):
x=myInterval/2.+float(i)*myInterval
ax.text(x,y,'{:d}'.format(i+j*nx),color='r',ha='center',va='center')
# Save the figure
#fig.savefig('myImageGrid.tiff',dpi=my_dpi)
newImageName = nameImageWithGrid(image.filename)
fig.savefig(newImageName,dpi=my_dpi)
return fig
However, when I run this code, the vertical grid lines stop appearing at a certain point. I have attached a screenshot of part of this image (the whole image is very large) to demonstrate the problem.
After some searching, I did find this previous issue so I am not sure if this is the same issue. It is worth noting that I am working with very large images, the one in the picture above has dimensions of 11648 × 8736.
I would appreciate some help on resolving this problem and making vertical grid lines appear on whole image.
EDIT:
I tried the code on another large image that can be found in this link here and had the same problem, here is a screenshot of part of the image where the vertical grid lines stop: