首页 > 解决方案 > 如何将 x、y 坐标从一个小比例图转换为一个更大尺寸的新 matplotlib 图?

问题描述

我正在使用 tkinter 在画布上手动绘制线条,然后记录 x、y 坐标。我想将这些 x,y 坐标转换为缩放比例非常不同的画布,并使用 matplotlib 绘制它们。我的 tkinter 画布目前尺寸为 690(x) x 490(y)。我想将这些坐标转换为尺寸为 x(范围从 -5300 到 5300)和 y(范围从 -3650 到 3650)的图形,其中零是图形的中心。我如何进行范围/比例转换?谢谢!

当前 tkinter 画布坐标范围:

root = tk.Tk()
background_image=tk.PhotoImage(file="Pitch2.png")
root.resizable(width=True, height=True)
root.wm_attributes("-topmost", 1)
canvas = tk.Canvas(root, bg="white", width=690, height=560)

所需的 matplotlib 范围:

fig,ax = plt.subplots()
plt.ylim(-3650, 3650)
plt.xlim(-5300, 5300)
filename = os.path.join(os.getcwd(),'Pitch.png')
im = plt.imread(filename)
graph_2, = ax.plot([], marker='.')

标签: pythonmatplotlibtkinter

解决方案


您的绘图中有 7,300 个像素matplotlib,即 [-3650,3650]。Tk您的图像中有 690 个。

因此,将您的Tkx 坐标乘以7300/690并将结果添加到 -3650 以获得matplotlibx 坐标:

mpltx = (Tkx * 7300/690) - 3650 

mplty = (Tky * 10600/560) - 5300

请记住 y坐标在 Numpy 中位于 x 坐标之前。


推荐阅读