【问题标题】:How do i change the position of the origin of the coordinates of my canvas?如何更改画布坐标原点的位置?
【发布时间】:2020-06-09 03:09:15
【问题描述】:

当我创建画布时 (0,0) 位于右上角。我需要一个笛卡尔坐标系,其原点位于窗口的底部中间。有没有办法改变坐标系?如果不是,我如何制作具有这些特征的新产品?

【问题讨论】:

  • 没有任何内置功能,因此您需要手动进行从逻辑系统到画布的坐标转换。它可以通过矩阵来完成,所以numpy 可能是一个不错的选择。
  • “当我创建画布时 (0,0) 位于右上角。” - 不,它是左上角。

标签: python canvas tkinter


【解决方案1】:

您可以使用网格。

例如见https://docs.scipy.org/doc/numpy/reference/generated/numpy.meshgrid.html

“meshgrid 对于评估网格上的函数非常有用。”

>>> import matplotlib.pyplot as plt
>>> import numpy as np
>>> x = np.arange(-5, 5, 0.1)
>>> y = np.arange(-5, 5, 0.1)
>>> xx, yy = np.meshgrid(x, y, sparse=True)
>>> z = np.sin(xx**2 + yy**2) / (xx**2 + yy**2)
>>> h = plt.contourf(x,y,z)
>>> plt.show()

或者不使用网格网格,您也可以使用两个向量来索引图像:

假设您的画布保存在变量im 中,您可以将原点位于底部中间的两个笛卡尔轴,然后使用它们来索引图像的像素:

x = np.arange(minx, maxx, 1)
y = np.arange(0, maxy, 1)

其中minxmaxx 应该是-(im.shape[1] / 2)(im.shape[1] / 2)(假设您的画布有偶数列。

最后得到一个坐标,例如coord=(your_x, your_y)来自im

im[np.where(y==coord[1])[0], np.where(x==coord[0])[0]]

【讨论】:

  • @BryanOakley 我可能误解了 OP 所说的“我的画布”是什么意思
猜你喜欢
  • 1970-01-01
  • 2010-11-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-14
  • 1970-01-01
  • 1970-01-01
  • 2013-10-17
  • 1970-01-01
相关资源
最近更新 更多