如何在 运行 a pygame window 时使用 matplotlib 显示图表?
How do I show a graph with matplotlib while running a pygame window?
我已经用 pygame 为一个大学项目编写了钟摆模拟程序,我想让用户使用 matplotlib window 查看图表。当我 运行 它来自 vscode 它 运行 完美,但是当我 运行 它直接来自终端并尝试查看它给我的图表时:
Fatal Python error: PyEval_SaveThread: the function must be called with the GIL held, but the GIL is released (the current Python thread state is NULL)
Python runtime state: initialized
Current thread 0x00007f00b05ac740 (most recent call first):
File "/usr/lib/python3/dist-packages/gi/overrides/Gtk.py", line 1649 in main
File "/home/estevao/.local/lib/python3.9/site-packages/matplotlib/backends/backend_gtk3.py", line 850 in mainloop
File "/home/estevao/.local/lib/python3.9/site-packages/matplotlib/backend_bases.py", line 3595 in show
File "/home/estevao/.local/lib/python3.9/site-packages/matplotlib/pyplot.py", line 378 in show
File "/home/estevao/Projetos/Pendulum/Estev\xe3o_Projeto1.py", line 27 in export_graph
File "/home/estevao/.local/lib/python3.9/site-packages/pygame_menu/widgets/core/widget.py", line 1230 in apply
File "/home/estevao/.local/lib/python3.9/site-packages/pygame_menu/widgets/widget/button.py", line 247 in update
File "/home/estevao/.local/lib/python3.9/site-packages/pygame_menu/menu.py", line 2322 in update
File "/home/estevao/Projetos/Pendulum/Estev\xe3o_Projeto1.py", line 325 in <module>
你知道我该如何解决吗?我可以上传整个代码,但它充满了混乱,而且足够大,如果你看到它,就不太可能帮助我。
实际上我必须通过更改 matplotlib 的后端并使用多处理来解决它。所以我在代码中所做的更改是
import matplotlib
matplotlib.use('TkAgg')
import multiprocessing
和
work = multiprocessing.Process(target=show_graph)
work.start()
其中 'show_graph' 是使用 matplotlib 生成和显示图形的函数。
我已经用 pygame 为一个大学项目编写了钟摆模拟程序,我想让用户使用 matplotlib window 查看图表。当我 运行 它来自 vscode 它 运行 完美,但是当我 运行 它直接来自终端并尝试查看它给我的图表时:
Fatal Python error: PyEval_SaveThread: the function must be called with the GIL held, but the GIL is released (the current Python thread state is NULL)
Python runtime state: initialized
Current thread 0x00007f00b05ac740 (most recent call first):
File "/usr/lib/python3/dist-packages/gi/overrides/Gtk.py", line 1649 in main
File "/home/estevao/.local/lib/python3.9/site-packages/matplotlib/backends/backend_gtk3.py", line 850 in mainloop
File "/home/estevao/.local/lib/python3.9/site-packages/matplotlib/backend_bases.py", line 3595 in show
File "/home/estevao/.local/lib/python3.9/site-packages/matplotlib/pyplot.py", line 378 in show
File "/home/estevao/Projetos/Pendulum/Estev\xe3o_Projeto1.py", line 27 in export_graph
File "/home/estevao/.local/lib/python3.9/site-packages/pygame_menu/widgets/core/widget.py", line 1230 in apply
File "/home/estevao/.local/lib/python3.9/site-packages/pygame_menu/widgets/widget/button.py", line 247 in update
File "/home/estevao/.local/lib/python3.9/site-packages/pygame_menu/menu.py", line 2322 in update
File "/home/estevao/Projetos/Pendulum/Estev\xe3o_Projeto1.py", line 325 in <module>
你知道我该如何解决吗?我可以上传整个代码,但它充满了混乱,而且足够大,如果你看到它,就不太可能帮助我。
实际上我必须通过更改 matplotlib 的后端并使用多处理来解决它。所以我在代码中所做的更改是
import matplotlib
matplotlib.use('TkAgg')
import multiprocessing
和
work = multiprocessing.Process(target=show_graph)
work.start()
其中 'show_graph' 是使用 matplotlib 生成和显示图形的函数。