0

Following on from this answer: https://stackoverflow.com/a/20471251/14736707, which adds labels to the grid squares from the top-left to bottom-right from 0 onward, how can I change this it so the labeling starts from the bottom-left, and works its way to the top-right?

The particular code I'm referring to is

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='w',ha='center',va='center')

Which results in the following:

Default labelling

What I'm after is the following, whereby depending on the image, I can return the value starting from a particular number - in this case X axis of 10 and Y of 12, working from the bottom-left.

Sorted X/Y

ext32
  • 45
  • 7
  • try in range (nx, -1, 1) https://stackoverflow.com/questions/869885/loop-backwards-using-indices – nate35 Jul 07 '22 at 16:17
  • @nate35 Thanks for the pointer. I've gone through the solutions on that page and unfortunately they don't reverse the numbering - the particular solution you provided returns no numbering – ext32 Jul 07 '22 at 16:27

1 Answers1

1

Change you code to

for j in range(ny):
    y=myInterval/2+j*myInterval
    for i in range(nx):
        x=myInterval/2+i*myInterval
        ax.text(x,y,f'{i+10}_{j+12}',color='b',ha='center',va='center')

Example based on the linked :

import matplotlib.pyplot as plt
import matplotlib.ticker as plticker

fig, ax = plt.subplots()
myInterval=100
loc = plticker.MultipleLocator(base=myInterval)
ax.xaxis.set_major_locator(loc)
ax.yaxis.set_major_locator(loc)
ax.xaxis.set_major_formatter(plticker.NullFormatter())
ax.yaxis.set_major_formatter(plticker.NullFormatter())
ax.tick_params(bottom=False, left=False)
ax.grid(which='major', axis='both', linestyle='-')
ax.set(xlim=(0,400), ylim=(0,400))

nx=abs(int((ax.get_xlim()[1]-ax.get_xlim()[0])/myInterval))
ny=abs(int((ax.get_ylim()[1]-ax.get_ylim()[0])/myInterval))

for j in range(ny):
    y=myInterval/2+j*myInterval
    for i in range(nx):
        x=myInterval/2+i*myInterval
        ax.text(x,y,f'{i+10}_{j+12}',color='b',ha='center',va='center')

enter image description here

Stef
  • 28,728
  • 2
  • 24
  • 52
  • Thank you. Strangely enough, when applying to a large image, say above 3000x3000px the image returns flipped upside down, but it works fine on smaller images as per the example. In addition, y-axis gridlines don't show for grids above x-axis 25 – ext32 Jul 07 '22 at 18:06