如何将 PyLab 与 PySimpleGUI 嵌入?

How can I embed PyLab with PySimpleGUI?

我检查了 GitHub 上 PySimpleGUI DemoPrograms 存储库的 Demo_Matplotlib.py 脚本,但找不到任何为将 PySimpleGUI 嵌入 PyLab 而创建的演示程序。如何将 PyLab 嵌入到我的 PySimpleGUI 代码中?

您可以改编现有的 Matplotlib 演示程序,并将绘图调用与 PyLab 绘图调用交换。

PySimpleGUI GitHub 演示程序页面上发布了一个新的 PyLab 演示: https://github.com/PySimpleGUI/PySimpleGUI/blob/master/DemoPrograms/Demo_Matplotlib_PyLab.py

要更改的代码的重要部分在顶部。如果要修改其他 Matplotlib 演示,请替换代码中注释指示的部分是 MatplotLib 代码的位置。

[编辑] - 在研究 PyLab 时,我 运行 穿过这个声明,我将把它添加到演示程序的评论中:

PyLab is a convenience module that bulk imports matplotlib.pyplot (for plotting) and NumPy (for Mathematics and working with arrays) in a single name space. Although many examples use PyLab, it is no longer recommended.

这段代码将在演示程序的 window:

中绘制一个 PyLab 图
from numpy import sin
from numpy import cos

x = pylab.linspace(-3, 3, 30)
y = x**2

pylab.plot(x, sin(x))
pylab.plot(x, cos(x), 'r-')
pylab.plot(x, -sin(x), 'g--')

fig = pylab.gcf()
figure_x, figure_y, figure_w, figure_h = fig.bbox.bounds

生成这张图