Home > Enterprise >  Re-arrange matplotlib grid text labels
Re-arrange matplotlib grid text labels

Time:07-08

Following on from this answer: 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

CodePudding user response:

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

  • Related