组织印

histoprint的Python项目详细描述


Scikit-HEP

漂亮的打印Numpy(和其他)柱状图到控制台

它是如何工作的?在

组织打印使用终端颜色代码和Unicode欺骗的混合(即。 (组合字符)绘制重叠的直方图。有些终端没有 能够正确显示Unicode组合字符。Histopront还是可以的 在这些终端中使用,但字符集需要约束到 非组合词(见下文)。在

示例

一些例子:

import numpy as np
from histoprint import text_hist, print_hist

A = np.random.randn(1000) - 2
B = np.random.randn(1000)
C = np.random.randn(1000) + 2
D = np.random.randn(500) * 2

# text_hist is a thin wrapper for numpy.histogram
text_hist(
    B, bins=[-5, -3, -2, -1, -0.5, 0, 0.5, 1, 2, 3, 5], title="Variable bin widths"
)

histA = np.histogram(A, bins=15, range=(-5, 5))
histB = np.histogram(B, bins=15, range=(-5, 5))
histC = np.histogram(C, bins=15, range=(-5, 5))
histD = np.histogram(D, bins=15, range=(-5, 5))
histAll = ([histA[0], histB[0], histC[0], histD[0]], histA[1])

# print_hist can be used to print multiple histograms at once
# (or just to print a single one as returned by numpy.histogram)
print_hist(histAll, title="Overlays", labels="ABCDE")
print_hist(
    histAll,
    title="Stacks",
    stack=True,
    symbols="      ",
    bg_colors="rgbcmy",
    labels="ABCDE",
)
print_hist(
    histAll,
    title="Summaries",
    symbols=r"=|\/",
    fg_colors="0",
    bg_colors="0",
    labels=["AAAAAAAAAAAAAAAA", "B", "CCCCCCCCCCCCC", "D"],
    summary=True,
)
examples.png

最后一个示例不使用端子颜色,因此可以将其复制为文本:

^{pr2}$

命令行界面

Histoprint还提供了一个简单的命令行界面来创建直方图 表格数据。它可以读入文件或直接从STDIN获取数据:

$ histoprint --help
Usage: histoprint [OPTIONS] INFILE

  Read INFILE and print a histogram of the contained columns.

  INFILE can be '-', in which case the data is read from STDIN.

Options:
  -b, --bins TEXT                 Number of bins or space-separated bin edges.
  -t, --title TEXT                Title of the histogram.
  --stack / --nostack             Stack the histograms.
  -s, --summary / -S, --nosummary
                                  Print summary statistics.
  -l, --label TEXT                Labels for the data, one for each column.
  --symbols TEXT                  Symbol cycle for multiple histograms.
                                  Choices & default: ' |=/\'

  --fg-colors TEXT                Colour cycle for foreground colours.
                                  Default: 'WWWWW', Choices:
                                  '0rgbcmykwRGBCMYKW'

  --bg-colors TEXT                Colour cycle for background colours.
                                  Default: 'K0000', Choices:
                                  '0rgbcmykwRGBCMYKW'

  -f, --field TEXT                Which fields to histogram. Interpretation of
                                  the fields depends on the file format. TXT
                                  files only support integers for column
                                  numbers starting at 0. For CSV files, the
                                  fields must be the names of the columns as
                                  specified in the first line of the file.
                                  When plotting from ROOT files, at least one
                                  field must be specified. This can either be
                                  the path to a single TH1, or one or more
                                  paths to TTree branches.

  -c, --columns INTEGER           Total width of the displayed historgram in
                                  characters. Defaults to width of the
                                  terminal.

  -r, --lines INTEGER             Approximate total height of the displayed
                                  historgram in characters. Calculated from
                                  number of columns by default.

  --version                       Show the version and exit.
  --help                          Show this message and exit.


$ histoprint -t "Title" -s -b "0.5 1.5 2.5 3.5 4.5" -l A -l B --fg-colors "0" --bg-colors "0" --symbols "|=" - <<EOF
1 2
2 3
2 3
2 nan
3 4
3 4
EOF
                                     Title
 5.00e-01 _
           │͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏
           │͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏
           │͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏
 1.50e+00 _│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏
           ╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏
           ╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏
           ╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏
 2.50e+00 _╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏│͏
           ╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏
           ╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏
           ╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏
 3.50e+00 _╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏╪͏
           ═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏
           ═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏
           ═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏
 4.50e+00 _═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏═͏
                               │͏ A         ═͏ B
                         Tot:  6.00e+00    5.00e+00
                         Avg:  2.17e+00    3.20e+00
                         Std:  6.87e-01    7.48e-01

支持其他直方图类型

柱状图可以直接绘制其他类型(更奇特的)柱状图 提供一种转换成Numpy格式的方法。目前这意味着他们 必须公开numpy()to_numpy()方法。两个TH1 拔出的直方图以及增强直方图的直方图是 支持方式如下:

import boost_histogram as bh
hist = bh.Histogram(bh.axis.Regular(20, -3, 3))
hist.fill(np.random.randn(1000))
print_hist(hist, title="Boost Histogram")

import uproot
file = uproot.open("http://scikit-hep.org/uproot/examples/Event.root")
hist = file["htime"]
print_hist(hist, title="uproot TH1")

如何得到它?在

$ pip install [--user] histoprint
$ conda install -c conda-forge histoprint

禁用Unicode组合字符

某些终端无法正确显示Unicode组合字符。 要禁用组合字符的使用,只需在调用时不要使用它们 print_hist

print_hist(some_histograms, symbols=" =|")

组合字符是/\。请注意,它们在 第4和第5个直方图的默认字符集(如果存在)。在

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java接口中的每个方法都是抽象的,但在抽象类中,我们也只能使用抽象方法   初始化Java中声明的、未初始化的变量会发生什么情况?   java BouncyCastle openPGP将字节[]数组加密为csv文件   在Java中将类A(和所有子类)映射到类B的实例的字典   RSA公钥编码,在Java和Android中,代码相同,结果不同   java在安卓中实现数字检测语音识别   java取消选择复选框   java如何在其他配置中重用Maven配置XML片段   java有没有一种有效的方法来检查HashMap是否包含映射到相同值的键?   spring处理程序调度失败;嵌套的例外是java。lang.NoClassDefFoundError:org/apache/http/client/HttpClient   带有ehcache的java多层缓存   java如何访问chromium(或任何其他浏览器)cookie   java通过将两个集合与spring data mongodb data中的条件合并来获取计数   安卓中R.java的语法错误