Я пытаюсь показать рисунок matplotlib.pyplot на Python 3.10, но не могу. Я знаю об этом вопросе и попробовал их ответы, но все еще безуспешно. The default OS distribution is Ubuntu 24.04 using Python 3.12 as a default.
Here is how I setup the Python 3.10 project venv and installed numpy and matplotlib:
$ uv add pyqt5
Resolved 15 packages in 89ms
Installed 3 packages in 45ms
+ pyqt5==5.15.11
+ pyqt5-qt5==5.15.16
+ pyqt5-sip==12.17.0
< /code>
Запуск того же сценария Python < /p>
$ /home/user/test_py310/.venv/bin/python /home/user/test_py310/test_matplotlib,py
qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "" even though it was found.
This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.
Available platform plugins are: eglfs, linuxfb, minimal, minimalegl, offscreen, vnc, wayland-egl, wayland, wayland-xcomposite-egl, wayland-xcomposite-glx, webgl, xcb.
Aborted (core dumped)
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
< /code>
дал эту ошибку: < /p>
$ /home/user/test_py310/.venv/bin/python /home/user/test_py310/test_matplotlib,py
AttributeError: module '_tkinter' has no attribute '__file__'. Did you mean: '__name__'?
The above exception was the direct cause of the following exception:
ImportError: failed to load tkinter functions
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/home/user/test_py310/test_matplotlib,py", line 9, in
plt.plot(x, y, label='sin(x)', color='blue', linestyle='--')
File "/home/user/test_py310/.venv/lib/python3.10/site-packages/matplotlib/pyplot.py", line 3827, in plot
return gca().plot(
File "/home/user/test_py310/.venv/lib/python3.10/site-packages/matplotlib/pyplot.py", line 2774, in gca
return gcf().gca()
File "/home/user/test_py310/.venv/lib/python3.10/site-packages/matplotlib/pyplot.py", line 1108, in gcf
return figure()
File "/home/user/test_py310/.venv/lib/python3.10/site-packages/matplotlib/pyplot.py", line 1042, in figure
manager = new_figure_manager(
File "/home/user/test_py310/.venv/lib/python3.10/site-packages/matplotlib/pyplot.py", line 551, in new_figure_manager
_warn_if_gui_out_of_main_thread()
File "/home/user/test_py310/.venv/lib/python3.10/site-packages/matplotlib/pyplot.py", line 528, in _warn_if_gui_out_of_main_thread
canvas_class = cast(type[FigureCanvasBase], _get_backend_mod().FigureCanvas)
File "/home/user/test_py310/.venv/lib/python3.10/site-packages/matplotlib/pyplot.py", line 369, in _get_backend_mod
switch_backend(rcParams._get("backend"))
File "/home/user/test_py310/.venv/lib/python3.10/site-packages/matplotlib/pyplot.py", line 425, in switch_backend
module = backend_registry.load_backend_module(newbackend)
File "/home/user/test_py310/.venv/lib/python3.10/site-packages/matplotlib/backends/registry.py", line 317, in load_backend_module
return importlib.import_module(module_name)
File "/home/user/.local/share/uv/python/cpython-3.10.16-linux-x86_64-gnu/lib/python3.10/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "", line 1050, in _gcd_import
File "", line 1027, in _find_and_load
File "", line 1006, in _find_and_load_unlocked
File "", line 688, in _load_unlocked
File "", line 883, in exec_module
File "", line 241, in _call_with_frames_removed
File "/home/user/test_py310/.venv/lib/python3.10/site-packages/matplotlib/backends/backend_tkagg.py", line 1, in
from . import _backend_tk
File "/home/user/test_py310/.venv/lib/python3.10/site-packages/matplotlib/backends/_backend_tk.py", line 25, in
from . import _tkagg
ImportError: initialization failed
< /code>
Использование < /p>
import matplotlib
matplotlib.use('Qt5Agg')
import matplotlib.pyplot as plt
< /code>
дал < /p>
$ /home/user/test_py310/.venv/bin/python /home/user/test_py310/test_matplotlib,py
qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "" even though it was found.
This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.
Available platform plugins are: eglfs, linuxfb, minimal, minimalegl, offscreen, vnc, wayland-egl, wayland, wayland-xcomposite-egl, wayland-xcomposite-glx, webgl, xcb.
Aborted (core dumped)
Я также удалил pyqt5 и добавил pyqt6 и использовал matplotlib.use ('qt6agg') , но получил эту ошибку:
$ /home/user/test_py310/.venv/bin/python /home/user/test_py310/test_matplotlib,py
Traceback (most recent call last):
File "/home/user/test_py310/test_matplotlib,py", line 4, in
matplotlib.use('Qt6Agg')
File "/home/user/test_py310/.venv/lib/python3.10/site-packages/matplotlib/__init__.py", line 1265, in use
name = rcsetup.validate_backend(backend)
File "/home/user/test_py310/.venv/lib/python3.10/site-packages/matplotlib/rcsetup.py", line 278, in validate_backend
raise ValueError(msg)
ValueError: 'Qt6Agg' is not a valid value for backend; supported values are ['gtk3agg', 'gtk3cairo', 'gtk4agg', 'gtk4cairo', 'macosx', 'nbagg', 'notebook', 'qtagg', 'qtcairo', 'qt5agg', 'qt5cairo', 'tkagg', 'tkcairo', 'webagg', 'wx', 'wxagg', 'wxcairo', 'agg', 'cairo', 'pdf', 'pgf', 'ps', 'svg', 'template']
Что я должен сделать, чтобы иметь возможность построить рисунок matplotlib.pyplot в виртуальной среде, которая установлена с помощью Python 3.10?>
Я пытаюсь показать рисунок matplotlib.pyplot на Python 3.10, но не могу. Я знаю об этом вопросе и попробовал их ответы, но все еще безуспешно. The default OS distribution is Ubuntu 24.04 using Python 3.12 as a default. Here is how I setup the Python 3.10 project venv and installed numpy and matplotlib: [code]$ uv init test_py310 --python 3.10 Initialized project `test-py310` at `/home/user/test_py310` $ cd test_py310/ $ uv add numpy matplotlib Using CPython 3.10.16 Creating virtual environment at: .venv Resolved 12 packages in 136ms Prepared 1 package in 1.96s Installed 11 packages in 43ms + contourpy==1.3.2 + cycler==0.12.1 + fonttools==4.57.0 + kiwisolver==1.4.8 + matplotlib==3.10.1 + numpy==2.2.5 + packaging==25.0 + pillow==11.2.1 + pyparsing==3.2.3 + python-dateutil==2.9.0.post0 + six==1.17.0 < /code> test_matplotlib.py: import numpy as np import matplotlib.pyplot as plt
x = np.linspace(0, 10, 100) y = np.sin(x) plt.plot(x, y, label='sin(x)', color='blue', linestyle='--') plt.show() < /code> ошибка: < /p> /home/user/Coding/test_py310/.venv/bin/python /home/user/test_py310/test_matplotlib,py /home/user/test_py310/test_matplotlib,py:7: UserWarning: FigureCanvasAgg is non-interactive, and thus cannot be shown plt.show() [/code] Далее я попытался установить pyqt5 в соответствии с этим ответом, но все еще обнаружил ошибку. [code]$ uv add pyqt5 Resolved 15 packages in 89ms Installed 3 packages in 45ms + pyqt5==5.15.11 + pyqt5-qt5==5.15.16 + pyqt5-sip==12.17.0 < /code> Запуск того же сценария Python < /p> $ /home/user/test_py310/.venv/bin/python /home/user/test_py310/test_matplotlib,py qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "" even though it was found. This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.
Aborted (core dumped) [/code] Изменение импорта matplotlib.pyplot как plt на: [code]import matplotlib matplotlib.use('TkAgg') import matplotlib.pyplot as plt < /code> дал эту ошибку: < /p> $ /home/user/test_py310/.venv/bin/python /home/user/test_py310/test_matplotlib,py AttributeError: module '_tkinter' has no attribute '__file__'. Did you mean: '__name__'?
The above exception was the direct cause of the following exception:
ImportError: failed to load tkinter functions
The above exception was the direct cause of the following exception:
Traceback (most recent call last): File "/home/user/test_py310/test_matplotlib,py", line 9, in plt.plot(x, y, label='sin(x)', color='blue', linestyle='--') File "/home/user/test_py310/.venv/lib/python3.10/site-packages/matplotlib/pyplot.py", line 3827, in plot return gca().plot( File "/home/user/test_py310/.venv/lib/python3.10/site-packages/matplotlib/pyplot.py", line 2774, in gca return gcf().gca() File "/home/user/test_py310/.venv/lib/python3.10/site-packages/matplotlib/pyplot.py", line 1108, in gcf return figure() File "/home/user/test_py310/.venv/lib/python3.10/site-packages/matplotlib/pyplot.py", line 1042, in figure manager = new_figure_manager( File "/home/user/test_py310/.venv/lib/python3.10/site-packages/matplotlib/pyplot.py", line 551, in new_figure_manager _warn_if_gui_out_of_main_thread() File "/home/user/test_py310/.venv/lib/python3.10/site-packages/matplotlib/pyplot.py", line 528, in _warn_if_gui_out_of_main_thread canvas_class = cast(type[FigureCanvasBase], _get_backend_mod().FigureCanvas) File "/home/user/test_py310/.venv/lib/python3.10/site-packages/matplotlib/pyplot.py", line 369, in _get_backend_mod switch_backend(rcParams._get("backend")) File "/home/user/test_py310/.venv/lib/python3.10/site-packages/matplotlib/pyplot.py", line 425, in switch_backend module = backend_registry.load_backend_module(newbackend) File "/home/user/test_py310/.venv/lib/python3.10/site-packages/matplotlib/backends/registry.py", line 317, in load_backend_module return importlib.import_module(module_name) File "/home/user/.local/share/uv/python/cpython-3.10.16-linux-x86_64-gnu/lib/python3.10/importlib/__init__.py", line 126, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "", line 1050, in _gcd_import File "", line 1027, in _find_and_load File "", line 1006, in _find_and_load_unlocked File "", line 688, in _load_unlocked File "", line 883, in exec_module File "", line 241, in _call_with_frames_removed File "/home/user/test_py310/.venv/lib/python3.10/site-packages/matplotlib/backends/backend_tkagg.py", line 1, in from . import _backend_tk File "/home/user/test_py310/.venv/lib/python3.10/site-packages/matplotlib/backends/_backend_tk.py", line 25, in from . import _tkagg ImportError: initialization failed < /code> Использование < /p> import matplotlib matplotlib.use('Qt5Agg') import matplotlib.pyplot as plt < /code> дал < /p> $ /home/user/test_py310/.venv/bin/python /home/user/test_py310/test_matplotlib,py qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "" even though it was found. This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.
Aborted (core dumped) [/code] Я также удалил pyqt5 и добавил pyqt6 и использовал matplotlib.use ('qt6agg') , но получил эту ошибку: [code]$ /home/user/test_py310/.venv/bin/python /home/user/test_py310/test_matplotlib,py Traceback (most recent call last): File "/home/user/test_py310/test_matplotlib,py", line 4, in matplotlib.use('Qt6Agg') File "/home/user/test_py310/.venv/lib/python3.10/site-packages/matplotlib/__init__.py", line 1265, in use name = rcsetup.validate_backend(backend) File "/home/user/test_py310/.venv/lib/python3.10/site-packages/matplotlib/rcsetup.py", line 278, in validate_backend raise ValueError(msg) ValueError: 'Qt6Agg' is not a valid value for backend; supported values are ['gtk3agg', 'gtk3cairo', 'gtk4agg', 'gtk4cairo', 'macosx', 'nbagg', 'notebook', 'qtagg', 'qtcairo', 'qt5agg', 'qt5cairo', 'tkagg', 'tkcairo', 'webagg', 'wx', 'wxagg', 'wxcairo', 'agg', 'cairo', 'pdf', 'pgf', 'ps', 'svg', 'template'] [/code] Что я должен сделать, чтобы иметь возможность построить рисунок matplotlib.pyplot в виртуальной среде, которая установлена с помощью Python 3.10?>
Я пытаюсь показать рисунок matplotlib.pyplot на Python 3.10, но не могу. Я знаю об этом вопросе и попробовал их ответы, но все еще безуспешно. The default OS distribution is Ubuntu 24.04 using Python 3.12 as a default.
Here is how I setup the Python...
Я пытаюсь показать рисунок matplotlib.pyplot на Python 3.10, но не могу. Я знаю об этом вопросе и попробовал их ответы, но все еще безуспешно. The default OS distribution is Ubuntu 24.04 using Python 3.12 as a default.
Here is how I setup the Python...
Я пытаюсь показать рисунок matplotlib.pyplot на Python 3.10, но не могу. Я знаю об этом вопросе и попробовал их ответы, но все еще безуспешно. The default OS distribution is Ubuntu 24.04 using Python 3.12 as a default.
Here is how I setup the Python...