使用Python matplotlib与MacOSX后端设置图形的绝对位置
有没有办法在使用MacOSX后端的matplotlib中设置图形在屏幕上的绝对位置?
这个回答
提到在其他后端可以做到,但没有说明如何在MacOSX后端实现。
1 个回答
5
在 MacOSX 系统上,使用 MacOSX 后端 的话,无法设置 matplotlib 窗口的位置。不过,通常在 MacOSX 上,你可以使用其他的 matplotlib 后端来实现这个功能。比如 TkAgg
后端,它是通过 Python 标准库中的 Tkinter 绑定使用 Tcl/Tk
的,这个后端应该会自动安装。
在你的 Python 脚本中,首先切换到这个后端,然后创建你的图表,显示它,这样你就可以用下面的代码来移动窗口:
get_current_fig_manager().window.wm_geometry("+<x-pos>+<y-pos>")
这里有一个可以运行的例子:
import matplotlib
matplotlib.use("TkAgg") # set the backend
import matplotlib.pyplot as plt
plt.figure()
plt.plot([0,1,2,0,1,2]) # draw something
plt.show(block=False)
plt.get_current_fig_manager().window.wm_geometry("+600+400") # move the window
如果你安装了 IMHO 更好看的 Qt4 图形用户界面框架,并使用 PyQt 绑定,那么你可以用下面的代码来设置窗口的位置:
get_current_fig_manager().window.setGeometry(<x-pos>,<x-pos>,<width>,<height>)
再次提供完整的例子:
import matplotlib
matplotlib.use("Qt4Agg") # set the backend
import matplotlib.pyplot as plt
plt.figure()
plt.plot([0,1,2,0,1,2]) # draw something
plt.show(block=False)
plt.get_current_fig_manager().window.setGeometry(600,400,1000,800)