步长的原因是什么?

发布于 2025-01-30 20:15:03 字数 757 浏览 2 评论 0原文

我正在尝试可视化Python的国际象棋板,我在网上找到了此代码,以便能够做到这一点,并且我正在尝试了解其工作原理。我对其中大多数的工作方式有一个粗略的了解,但是我不明白为什么DX和DY是这些奇怪的价值观? 我已经尝试了1个值1,它使数字不合时宜?

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()

有人可以解释吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

始终不够爱げ你 2025-02-06 20:15:03

我认为有人在这里过度复杂化。如果您注意到,xy根本不使用,而xy仅用于计算范围。但这仅需要第一个和最后一个元素。当[0][ - 1] 也一样。这些阵列可能会在其他地方用于其他东西。如果您只想可视化板,则以下内容就足够了:

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()

​这意味着,如果我想要与数字1-8相对应的像素中心,我必须将界限设置为0.5至8.5,因为每个图像像素的数据大小为1。在Y轴上更容易。原始代码将x范围设置为-4,3.995y -4,3.95,这对我来说毫无意义,因为它与正方形等方面不​​一致。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文