Python ASCII plots in terminal
You can also try Sympy's TextBackend
for plots, see doc. Or just use textplot
.
Here it is an example
from sympy import symbols
from sympy.plotting import textplot
x = symbols('x')
textplot(x**2,0,5)
with the output
24.0992 | /
| ..
| /
| ..
| ..
| /
| ..
| ..
12.0496 | ---------------------------------------..--------------
| ...
| ..
| ..
| ...
| ...
| ...
| .....
| .....
0 | .............
0 2.5 5
As few answers already suggested the gnuplot
is a great choice.
However, there is no need to call a gnuplot subprocess, it might be much easier to use a python gnuplotlib
library.
Example (from: https://github.com/dkogan/gnuplotlib):
>>> import numpy as np
>>> import gnuplotlib as gp
>>> x = np.linspace(-5,5,100)
>>> gp.plot( x, np.sin(x) )
[ graphical plot pops up showing a simple sinusoid ]
>>> gp.plot( (x, np.sin(x), {'with': 'boxes'}),
... (x, np.cos(x), {'legend': 'cosine'}),
... _with = 'lines',
... terminal = 'dumb 80,40',
... unset = 'grid')
[ ascii plot printed on STDOUT]
1 +-+---------+----------+-----------+-----------+----------+---------+-+
+ +|||+ + + +++++ +++|||+ + +
| |||||+ + + +|||||| cosine +-----+ |
0.8 +-+ |||||| + + ++||||||+ +-+
| ||||||+ + ++||||||||+ |
| ||||||| + ++||||||||| |
| |||||||+ + ||||||||||| |
0.6 +-+ |||||||| + +||||||||||+ +-+
| ||||||||+ | ++||||||||||| |
| ||||||||| + ||||||||||||| |
0.4 +-+ ||||||||| | ++||||||||||||+ +-+
| ||||||||| + +|||||||||||||| |
| |||||||||+ + ||||||||||||||| |
| ||||||||||+ | ++||||||||||||||+ + |
0.2 +-+ ||||||||||| + ||||||||||||||||| + +-+
| ||||||||||| | +||||||||||||||||+ | |
| ||||||||||| + |||||||||||||||||| + |
0 +-+ +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ +-+
| + ||||||||||||||||||+ | ++|||||||||| |
| | +||||||||||||||||| + ||||||||||| |
| + ++|||||||||||||||| | +|||||||||| |
-0.2 +-+ + ||||||||||||||||| + ||||||||||| +-+
| | ++||||||||||||||+ | ++||||||||| |
| + ||||||||||||||| + ++|||||||| |
| | +|||||||||||||| + ||||||||| |
-0.4 +-+ + ++||||||||||||+ | +|||||||| +-+
| + ||||||||||||| + ||||||||| |
| | +|||||||||||+ + ++||||||| |
-0.6 +-+ + ++|||||||||| | +||||||| +-+
| + ||||||||||| + ++|||||| |
| + +|||||||||+ + ||||||| |
| + ++|||||||| + +++||||| |
-0.8 +-+ + + ++||||||+ + + +||||| +-+
| + + +|||||| + + ++|||| |
+ + + ++ ++|||++ + + ++ + + ++||| +
-1 +-+---------+----------+-----------+-----------+----------+---------+-+
-6 -4 -2 0 2 4 6
As @Benjamin Barenblat pointed out, there is currently no way using matplotlib. If you really want to use a pure python library, you may check ASCII Plotter. However, as I commented above, I would use gnuplot as suggested e.g. in this question.
To use gnuplot directly from python you could either use Gnuplot.py (I haven't tested this yet) or use gnuplot with the scripting interface. Latter can be realised (as suggested here) like:
import numpy as np
x=np.linspace(0,2*np.pi,10)
y=np.sin(x)
import subprocess
gnuplot = subprocess.Popen(["/usr/bin/gnuplot"],
stdin=subprocess.PIPE)
gnuplot.stdin.write("set term dumb 79 25\n")
gnuplot.stdin.write("plot '-' using 1:2 title 'Line1' with linespoints \n")
for i,j in zip(x,y):
gnuplot.stdin.write("%f %f\n" % (i,j))
gnuplot.stdin.write("e\n")
gnuplot.stdin.flush()
This gives a plot like
1 ++--------+---A******---------+--------+---------+---------+--------++
+ + ** +A* + + + Line1 **A*** +
0.8 ++ ** * ++
| ** ** |
0.6 ++ A * ++
| * * |
0.4 ++ * ++
| ** A |
0.2 ++* * ++
|* * |
0 A+ * A ++
| * * |
-0.2 ++ * * ++
| A* ** |
-0.4 ++ * * ++
| ** * |
-0.6 ++ * A ++
| * ** |
-0.8 ++ ** ++
+ + + + + A****** ** + +
-1 ++--------+---------+---------+--------+--------A+---------+--------++
0 1 2 3 4 5 6 7
Some styling options can be found e.g. here.