What is the reasoning for the step size?

Viewed 52

I am trying to visualise a chess board in python, I have found this code online in order to be able to do so and I am trying to understand how it works. I have a rough idea of how majority of it works but I can't understand why dx and dy are these odd values? I have tried it with a value of 1 and it makes the axis for the numbers out of line?

import matplotlib.pyplot as plt
import numpy as np

from matplotlib.colors import LogNorm

dx, dy = 0.015, 0.05
x = np.arange(-4.0, 4.0, dx) # Numbers from -4 to 4 incrementing by 0.015
print(x)
y = np.arange(-4.0, 4.0, dy) # Numbers from -4 to 4 incrementing by 0.05
X, Y = np.meshgrid(x, y) # Generates an array using the values
extent = np.min(x), np.max(x), np.min(y), np.max(y) #Max and minimum X,Y values
z1 = np.add.outer(range(8), range(8)) % 2 #Generates colouring for the grid
print(z1)
plt.imshow(z1, cmap="Greys", interpolation="nearest", extent=extent, alpha=1)
plt.show()

Can someone explain please?

1 Answers

I think someone is overcomplicating things here. If you notice, X, Y are not used at all, while x and y are only used to compute extent. But that only requires the first and last element. There is no need to call min and max on a sorted array, when [0] and [-1] will do just as well. It's possible that these arrays are used elsewhere for something else. If you want to just visualize the board, the following is sufficient:

z = np.add.outer(range(8), range(8)) % 2 #Generates colouring for the grid
plt.imshow(z, cmap="Greys", extent=[0.5, 8.5, 0.5, 8.5])
plt.xticks(range(1, 9), 'ABCDEFGH')
plt.show()

enter image description here

Image extents are set to the outer bounds of each pixel. That means that if I want my pixel centers corresponding to the numbers 1-8, I have to set the bounds to be 0.5 to 8.5, since each image pixel is to have a data-size of 1. You can see why this is the case more easily on the y-axis. The original code is setting the x extent to -4, 3.995 and the y to -4, 3.95, which makes little sense to me, as it does not align integer positions with the squares, among other issues.

Related