终端中的Python ASCII图
使用Octave,我能够在终端上绘制数组,例如,绘制带有函数值的数组将x^2
在我的终端上显示以下输出:
10000 ++---------+-----------+----------+-----------+---------++
++ + + + + ++
|+ : : : : +|
|++ : : : : ++|
| + : : : : + |
| ++ : : : : ++ |
8000 ++.+..................................................+.++
| ++ : : : : ++ |
| ++ : : : : ++ |
| + : : : : + |
| ++ : : : : ++ |
| + : : : : + |
6000 ++....++..........................................++....++
| ++ : : : : ++ |
| + : : : : + |
| ++ : : : : ++ |
| ++: : : :++ |
4000 ++........++..................................++........++
| + : : + |
| ++ : : ++ |
| :++ : : ++: |
| : ++ : : ++ : |
| : ++ : : ++ : |
2000 ++.............++........................++.............++
| : ++ : : ++ : |
| : +++ : : +++ : |
| : ++ : : ++ : |
| : +++: :+++ : |
+ + ++++ ++++ + +
0 ++---------+-----------+----------+-----------+---------++
0 20000 40000 60000 80000 100000
有什么方法可以在Python中完成类似的操作,特别是使用matplotlib吗?bashplotlib似乎提供了其中一些功能,但与Octave的产品相比似乎是非常基础的。
-
正如已经给出的几个答案所建议的那样,这
gnuplot
是一个不错的选择。但是,无需调用gnuplot子进程,使用python
gnuplotlib
库可能会容易得多。示例(来自: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