def plot_in_terminal(expr, *args, prec=None, logname=None, file=None, **kwargs):
"""
Run mpmath.plot() but show in terminal if possible
"""
from mpmath import plot
if logname:
os.makedirs('plots', exist_ok=True)
file = 'plots/%s.png' % logname
if prec:
mpmath.mp.dps = prec
if isinstance(expr, (list, tuple)):
f = [lambdify(t, i, 'mpmath') for i in expr]
else:
f = lambdify(t, expr, 'mpmath')
try:
if hasattr(sys.stdout, 'isatty') and sys.stdout.isatty():
from iterm2_tools.images import display_image_bytes
else:
raise ImportError
except ImportError:
plot(f, *args, file=file, **kwargs)
else:
# mpmath.plot ignores the axes argument if file is given, so let
# file=False, disable this.
if 'axes' in kwargs:
file=False
if file is not False:
from io import BytesIO
b = BytesIO()
else:
b = None
plot(f, *args, **kwargs, file=b)
if file:
with open(file, 'wb') as f:
f.write(b.getvalue())
if b:
print(display_image_bytes(b.getvalue()))
评论列表
文章目录