📈 如何在 Termux 上安装 Matplotlib

本教程教你如何在 Termux 上安装 Matplotlib,无需等待编译,直接使用预编译的 wheel 包。

⚡ 为什么要用预编译源?

❌ 不用预编译源(官方 PyPI)

$ pip install matplotlib
Collecting matplotlib
  Downloading matplotlib-3.10.8.tar.gz (53.2 MB)
  Installing build dependencies ... 
  ⚠️ 需要编译许多依赖(freetype, qhull, 等),在手机上几乎不可能完成

结果:编译依赖太多,内存不足经常失败 ❌

✅ 使用预编译源(本仓库)

$ pip install --index-url https://nsyhykui.github.io/python_wheels_for_termux/simple/ matplotlib
Looking in indexes: https://nsyhykui.github.io/.../simple/
Collecting matplotlib
  Downloading matplotlib-3.10.8-cp313-cp313-android_24_arm64_v8a.whl (10.2 MB)
Downloading numpy-2.4.4-cp313-cp313-android_24_arm64_v8a.whl (5.7 MB)
Successfully installed matplotlib-3.10.8 numpy-2.4.4 ...

real    2m15.789s

结果:2 分钟装好 ✅

⚡ 一键设为默认源(以后不用再敲 --index-url)
pip config set global.index-url https://nsyhykui.github.io/python_wheels_for_termux/simple/
设置后,直接 pip install matplotlib 即可。

📦 一行命令安装

pip install --index-url https://nsyhykui.github.io/python_wheels_for_termux/simple/ matplotlib
⚠️ 关于绘图后端(重要)

Matplotlib 默认可能需要图形界面。如果你在 Termux 中运行时遇到 tkinterqt 相关错误:
import matplotlib
matplotlib.use('Agg')  # 切换到无图形界面后端
import matplotlib.pyplot as plt
或者,如果你已经安装了 Termux:X11VNC,可以正常使用默认后端。

✅ 验证安装

python -c "import matplotlib; print(matplotlib.__version__)"

📊 快速测试绘图(无界面模式)

python -c "
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
plt.plot([1,2,3], [4,5,6])
plt.savefig('test.png')
print('绘图成功,保存为 test.png')
"

🔧 常见问题


🔗 ← 安装 NumPy | 安装 Pandas | 安装 SciPy | 安装 Jupyter | 返回教程首页 | 包索引