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

如何在本地Windows设备显示远程Ubuntu平台的matplotlib.pyplot绘图?

解决Ubuntu 18.04.6 + Python 2.7下Matplotlib绘图显示问题

1. 修复默认Tk后端缺失问题

Ubuntu系统中Python 2.7的_tkinter模块无法通过pip安装,必须使用系统包管理器安装对应依赖:

sudo apt update
sudo apt install python-tk

安装完成后,直接导入matplotlib.pyplot即可使用默认Tk后端正常显示绘图。

2. 替代后端方案

方案A:GTK2后端

Ubuntu 18.04自带Python2.7的GTK2绑定,安装系统包即可:

sudo apt install python-gtk2 python-gtk2-dev

代码中指定后端并测试:

import matplotlib
matplotlib.use('GTK2Agg')
import matplotlib.pyplot as plt

plt.plot([1, 2, 3])
plt.show()

方案B:AGG后端生成图像文件

AGG是无界面后端,若无需实时GUI显示,可生成图像文件后查看:

import matplotlib
matplotlib.use('agg')
import matplotlib.pyplot as plt

plt.plot([1, 2, 3])
plt.savefig('plot.png')
# 用系统图像查看器打开plot.png即可查看结果

方案C:nbagg后端(IPython/Jupyter环境)

若使用IPython或Jupyter Notebook,通过系统包安装对应版本:

sudo apt install ipython ipython-notebook

在IPython或Notebook中启用nbagg后端:

%matplotlib nbagg
import matplotlib.pyplot as plt

plt.plot([1, 2, 3])
plt.show()

内容的提问来源于stack exchange,提问作者bob.sacamento

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.02 20:02:49