Skip to main content

一个简单的 python 库,用于将数据打印为 ascii 直方图。

项目描述

py-ascii-graph

一个简单的 python 库,用于将数据打印为 ascii 直方图

特拉维斯 CI PyPI 版本 在 https://gitter.im/kakwa/py-ascii-graph 加入聊天 https://coveralls.io/repos/kakwa/py-ascii-graph/badge.svg?branch=master 文件状态
吉特

Github

派皮

包裹

文件

文档

执照

麻省理工学院

作者

Pierre-Francois Carpentier - 版权所有 2014


执照

py-ascii-graph 在 MIT 许可下发布。

描述

py-ascii-graph 是一个简单的 python 库,用于构建 ascii 直方图。只需给它一个标签和一个元组列表(描述,值),它就会自动创建一个漂亮的直方图,所有的东西都对齐并适合固定宽度的线(如果可能的话)。

py-ascii-graph 虽然带有命令行实用程序。

例子

图书馆

简单的例子:

from ascii_graph import Pyasciigraph

test = [('long_label', 423), ('sl', 1234), ('line3', 531),
    ('line4', 200), ('line5', 834)]

graph = Pyasciigraph()
for line in  graph.graph('test print', test):
    print(line)

结果:

test print
###############################################################################
████████████████████                                            423  long_label
█████████████████████████████████████████████████████████████  1234  sl
██████████████████████████                                      531  line3
█████████                                                       200  line4
█████████████████████████████████████████                       834  line5

复杂示例(颜色、不同间距、无标签……):

from ascii_graph import Pyasciigraph
from ascii_graph.colors import *
from ascii_graph.colordata import vcolor
from ascii_graph.colordata import hcolor

test = [('long_label', 423), ('sl', 1234), ('line3', 531),
    ('line4', 200), ('line5', 834)]

# One color per line
print('Color example:')
pattern = [Gre, Yel, Red]
data = vcolor(test, pattern)

graph = Pyasciigraph()
for line in graph.graph('vcolor test', data):
    print(line)

# Multicolor on one line
print('\nMultiColor example:')

# Color lines according to Thresholds
thresholds = {
  51:  Gre, 100: Blu, 350: Yel, 500: Red,
}
data = hcolor(test, thresholds)

# graph with colors, power of 1000, different graph symbol,
# float formatting and a few tweaks
graph = Pyasciigraph(
    line_length=120,
    min_graph_length=50,
    separator_length=4,
    multivalue=False,
    human_readable='si',
    graphsymbol='*',
    float_format='{0:,.2f}',
    force_max_value=2000,
    )

for line in graph.graph(label=None, data=data):
    print(line)

命令行实用程序

命令行:

$ asciigraph -h
Usage: asciigraph [-l <label>] [-f file] [-s inc|dec] \
   [-c] [-t <first color threshold> [-T <second color threshold>] \
   [-w <number of char>] [-m <min len of char>] [-H] [-M cs|si]

examples:
   printf 'label1:10\nlabel2:100\n' | asciigraph -l 'my graph'
   printf 'label1:1000\nlabel2:20000\n' | asciigraph -l 'my graph' -H -M 'si'
   printf 'l1:100\nl2:1200.42\n' > ./mf; asciigraph -l 'my graph' -f ./mf
   asciigraph -l 'my graph' -f mf -s inc
   asciigraph -l 'my graph' -f mf -s dec -w 60 -m 10
   asciigraph -l 'my graph' -f mf -c -F '{0:,.2f}'
   asciigraph -l 'my graph' -f mf -c -t 5 -T 50


Options:
  -h, --help            show this help message and exit
  -f FILE, --file=FILE  import data from FILE (one data per line,
                        format: <label>:<value>)
  -s SORT, --sort=SORT  sort type: inc (increasing) or dec (decreasing)
  -l LAB, --label=LAB   label of the graph
  -w WIDTH, --width=WIDTH
                        width of the graph
  -m LEN, --min_graph=LEN
                        minimum length of the graph bar
  -c, --color           Color the graph
  -t TC1, --threshold-1=TC1
                        first color threshold, only make sense if --color is
                        passed
  -T TC2, --threshold-2=TC2
                        second color threshold, only make sense if --color is
                        passed
  -H, --human-readable  enable human readable mode (K, M, G, etc)
  -M HR_MODE, --human-readable-mode=HR_MODE
                        Human readable mode ('cs' -> power of 1024 or 'si' ->
                        power of 1000, default: cs)
  -F FORMAT, --float-format=FORMAT
                        float formatting, ex: {0:,.2f}

有关更多示例,请参见示例/目录。

安装

$ pip install ascii_graph

或者

$ easy_install ascii_graph

项目详情


下载文件

下载适用于您平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。

源分布

ascii_graph-1.5.1.tar.gz (24.0 kB 查看哈希

已上传 source