问题描述
环境
- 操作系统:在 Windows 11 的 WSL2 中运行的 Ubuntu 24.04。配置方法见此文。
- 开发环境:Python 3.13,Matplotlib 3.10。是在 Mar. 11, 2025 用包管理器 conda 通过
conda create --prefix ./env matplotlib
命令创建、conda activate ./env
命令激活的。
最小工作实例
创建一个 Python 脚本,内容为(复制从此处)
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 2 * np.pi, 200)
y = np.sin(x)
fig, ax = plt.subplots()
ax.plot(x, y)
plt.show()
执行之,无图片显示,获得报错信息
qt.qpa.plugin: Could not find the Qt platform plugin "wayland" in ""
qt.qpa.plugin: From 6.5.0, xcb-cursore or libxcb-cursore is needed to load the Qt xcb platform plugin.
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: xcb, vnc, eglfs, offscreen, minimalegl, minimal, linuxfb.
Aborted (core dumped)
解决方案
报错信息说找不到 Qt platform 的 wayland 插件。据此,用 conda 安装 Qt Wayland 模块在虚拟环境中,
conda install conda-forge::qt-wayland
安装完毕,重新运行前述脚本,可见交互式图窗被正确创建。