步长的原因是什么?
我正在尝试可视化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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为有人在这里过度复杂化。如果您注意到,
x
,y
根本不使用,而x
和y
仅用于计算范围
。但这仅需要第一个和最后一个元素。当[0]
和[ - 1] 也一样。这些阵列可能会在其他地方用于其他东西。如果您只想可视化板,则以下内容就足够了:
这意味着,如果我想要与数字1-8相对应的像素中心,我必须将界限设置为0.5至8.5,因为每个图像像素的数据大小为1。在Y轴上更容易。原始代码将
x
范围设置为-4,3.995
,y
-4,3.95
,这对我来说毫无意义,因为它与正方形等方面不一致。I think someone is overcomplicating things here. If you notice,
X
,Y
are not used at all, whilex
andy
are only used to computeextent
. But that only requires the first and last element. There is no need to callmin
andmax
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: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 they
to-4, 3.95
, which makes little sense to me, as it does not align integer positions with the squares, among other issues.