I just got this running for myself and these are the steps I used:
- install termux (from the Play Store or FDroid)
- install python in termux:
pkg install python
- install matplotlib
- install a graphical environment for matplotlib (I installed tkinter):
pkg install python-tkinter
- install an X server
(I used XServer XSDL)
- use this initialization sequence in your python:
import os
from time import sleep
os.environ["MPLBACKEND"] = "TkAgg"
os.environ["DISPLAY"] = ":0.0"
print('Loading X server...')
os.system("am start --user 0 -n x.org.server/.RunFromOtherApp 2>/dev/null")
os.environ["DISPLAY"] = ":0.0"
sleep(8) # give the X server an opportunity to start
print('Done')
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(0, 5, 0.1);
y = np.sin(x)
plt.plot(x, y)
plt.show()
There's probably a better way to detect whether the X server is already running so you don't have to launch it every time, but I haven't figured out how to do that yet.