python - python 中的 Ascii 图

标签 python

我有一个字典 band1 如下所示,我想根据字典中每个列表的第一个和最后一个元素打印出一个图表。 x 轴上每个列表的第一个元素是频率,最后一个元素是接收强度,应位于 y 轴上。 例如,10812 的强度为 16 等

band1 = {'channel1': [10564, 2112.8, 1922.8, 0],
         'channel10': [10787, 2157.4, 1967.4, 11],
         'channel11': [10812, 2162.4, 1972.4, 16],
         'channel12': [10837, 2167.4, 1977.4, 46],
         'channel2': [10589, 2117.8, 1927.8, 29],
         'channel3': [10612, 2122.4, 1932.4, 0],
         'channel4': [10637, 2127.4, 1937.4, 40],
         'channel5': [10662, 2132.4, 1942.4, 54],
         'channel6': [10687, 2137.4, 1947.4, 0],
         'channel7': [10712, 2142.4, 1952.4, 50],
         'channel8': [10737, 2147.4, 1957.4, 19],
         'channel9': [10762, 2152.4, 1962.4, 24]}

我对此进行排序没有问题,channel1 -> channel12 但是有哪些打印漂亮图表的好方法,字典中的条目数量可能会随着 channel 的增加或减少而变化。

最佳答案

这是点图的算法,尽可能简单和天真。当然,它的性能远未实现,可以优化,输出可以有一些轴和数字。

HEIGHT = 10
WIDTH = 40
MARKER = '*'
FILL_CHARACTER = ' '

coords = [(ch[0], ch[3]) for ch in band1.values()]


# Convert to coordinates of a desired ASCII area

xmin = min(c[0] for c in coords)
xmax = max(c[0] for c in coords)
kx = (WIDTH - 1)  / (xmax - xmin)

ymin = min(c[1] for c in coords)
ymax = max(c[1] for c in coords)
ky = (HEIGHT - 1) / (ymax - ymin)

acoords = [(round((c[0] - xmin) * kx),
            round((c[1] - ymin) * ky)) for c in coords]

# Actually draw the graph

for y in range(HEIGHT, -1, -1):
    chars = []
    for x in range(WIDTH):
        if (x, y) in acoords:
            chars.append(MARKER)
        else:
            chars.append(FILL_CHARACTER)
    print(''.join(chars))

结果:

              *                         
                     *                 *
          *                             

    *                                   
                            *           
                         *         *    
                                *       

*      *          *                     

如果 x 坐标是唯一的,可以很容易地修改它来绘制柱状图或线状图。

例如对于酒吧的情况:

HEIGHT = 10
WIDTH = 40
MARKER = '*'
FILL_CHARACTER = ' '

coords = [(ch[0], ch[3]) for ch in band1.values()]
coords.sort(key=lambda ch: ch[1])

xmin = min(c[0] for c in coords)
xmax = max(c[0] for c in coords)
kx = (WIDTH - 1)  / (xmax - xmin)

ymin = min(c[1] for c in coords)
ymax = max(c[1] for c in coords)
ky = (HEIGHT - 1) / (ymax - ymin)

acoords = {}
for c in coords:
    x = round((c[0] - xmin) * kx)
    y = round((c[1] - ymin) * ky)
    if x not in acoords:
        acoords[x] = y
    else:
        acoords[x] = max(acoords[x], y)

for y in range(HEIGHT, -1, -1):
    chars = []
    for x in range(WIDTH):
        if acoords.get(x, 0) >= y:
            chars.append(MARKER)
        else:
            chars.append(FILL_CHARACTER)
    print(''.join(chars))

结果:

              *                         
              *      *                 *
          *   *      *                 *
          *   *      *                 *
    *     *   *      *                 *
    *     *   *      *      *          *
    *     *   *      *   *  *      *   *
    *     *   *      *   *  *   *  *   *
    *     *   *      *   *  *   *  *   *
****************************************

关于python - python 中的 Ascii 图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16240358/

相关文章:

python - Django 和杰通

python - 在 python 3.9+ 中,如何使用省略号为内置元组类型编写类型别名? (mypy 错误?)

python - Django 指向错误版本的 Postgres

python - 当 unique_together 应用于 django-rest 时,SlugRelatedField 的查询集值

python - 从数据框中删除反向重复项

python - 为什么 tempfile 在我的 XP 机器上使用 DOS 8.3 目录名称?

python - 如何在 Python 中将原始日期时间转换为支持 DST 的日期时间?

python - 将在 python 中训练的 xgboost 模型加载到 scala

python - 杀死执行多个子进程的python脚本

python - 如何避免循环回到代码中的行?