【问题标题】:How do I convert x, y coordinates from one graph with a small scale to a new matplotlib graph of a much greater size?如何将 x、y 坐标从一个小比例图转换为一个更大尺寸的新 matplotlib 图?
【发布时间】:2020-11-23 20:54:10
【问题描述】:

我正在使用 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='.')

【问题讨论】:

    标签: python matplotlib tkinter


    【解决方案1】:

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

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

    mpltx = (Tkx * 7300/690) - 3650 
    
    mplty = (Tky * 10600/560) - 5300
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-25
      • 1970-01-01
      • 1970-01-01
      • 2016-09-06
      • 1970-01-01
      相关资源
      最近更新 更多