1

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.

enter image description here

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: enter image description here

sums22
  • 1,793
  • 3
  • 13
  • 25
  • It doesn't change the fact that this is caused by a huge image, but don't we need a huge image that can be provided to users to confirm the same event? – r-beginners Jul 26 '21 at 12:23
  • @r-beginners I tried the code on another large image (link above) and ran into the same problem with the disappearing grid lines. Any idea why? – sums22 Jul 26 '21 at 12:42

1 Answers1

1

I believe that the locator is the cause of the issues that are occurring now. The grid lines are drawn for the ticks created by the interval values. For example, if you change the tick marks on the x-axis like this, the grid lines are drawn correctly.

# 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)
ax.set_xticks(np.arange(0,9900,myInterval))
r-beginners
  • 31,170
  • 3
  • 14
  • 32
  • You can set the maximum value manually, but you need to fix it there. – r-beginners Jul 26 '21 at 14:00
  • The code above fixes the problem with the vertical grid lines. However, the horizontal grid lines are no longer drawn correctly. Therefore, I would uncomment the loc line and the line ax.yaxis.set_major(loc) so the x and y ticks are all set correctly. – sums22 Jul 28 '21 at 10:02