You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VSCode中plt.show()无法显示图像的问题排查与解决请求

解决Matplotlib在VSCode中无法显示图像的问题

一、检查Matplotlib后端设置

Matplotlib的后端负责图像渲染,不同系统需匹配对应后端:

  • 在代码开头添加后端设置(优先尝试):
    import matplotlib
    # Windows/macOS可试TkAgg,Linux可试Qt5Agg
    matplotlib.use('TkAgg')
    
    注意要在导入pyplot之前执行该设置。

二、调整VSCode相关设置

  • 打开VSCode设置,搜索Python: Matplotlib Plotting,将选项改为interactive或inline,避免选择none。
  • 确保安装ipykernel,VSCode交互式运行依赖该包:
    pip install ipykernel
    

三、修复Matplotlib包

  • 卸载重装Matplotlib,解决可能的包损坏问题:
    pip uninstall matplotlib -y
    pip install matplotlib
    
  • Linux用户需确认安装tkinter依赖:
    sudo apt-get install python3-tk  # Ubuntu/Debian系
    

四、修改后的测试代码

# 创建测试数据集
import matplotlib
matplotlib.use('TkAgg')  # 按需替换后端
from sklearn.datasets import make_moons
from matplotlib import pyplot as plt
from matplotlib import style

X, y = make_moons(n_samples=1000, noise=0.1)
plt.scatter(X[:, 0], X[:, 1], s=40, color='g')
plt.xlabel("X")
plt.ylabel("Y")

plt.show()

排查小技巧

  • 直接在系统终端运行脚本,若能显示图像,说明问题出在VSCode设置;若仍不显示,排查Matplotlib本身或系统依赖。

内容的提问来源于stack exchange,提问作者Leshui

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 03:15:32