使 Python 程序中 matplotlib.pyplot 库的 show() 语句在 WSL2 中正确执行的方案

问题描述

环境

最小工作实例

创建一个 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

安装完毕,重新运行前述脚本,可见交互式图窗被正确创建。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值