python - 如何将 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='.')
解决方案
您的绘图中有 7,300 个像素matplotlib
,即 [-3650,3650]。Tk
您的图像中有 690 个。
因此,将您的Tk
x 坐标乘以7300/690
并将结果添加到 -3650 以获得matplotlib
x 坐标:
mpltx = (Tkx * 7300/690) - 3650
mplty = (Tky * 10600/560) - 5300
请记住 y坐标在 Numpy 中位于 x 坐标之前。
推荐阅读
- java - Junit 5 的 assertEquals 精度为 double
- java - Selenium - Java:有没有办法使用 WebElement 而不是按类获取定位器
- python - 有没有我可以在 python 中使用的函数来实现 Matlab 函数 qtdecomp?
- dns - 为什么域名中的资源(例如hello.jpg)访问无效,但IP地址有效
- ios - Swift:如何在返回类型为 self 的协议中声明 func?
- html - CSS绝对位置子菜单在父容器上显示滚动条
- php - 我无法在我的自定义插件 php 类中挂接到 wpcf7_mail_sent
- javascript - 挂载方法 - Vue.js
- python - Matplotlib -- twiny:如何在一个图中对齐两个 x 轴的值?
- asp.net - 如何在 Visual Studio (sln) 解决方案中创建 .env 文件