Last active
June 5, 2017 14:44
-
-
Save nickp60/239c9a56b35576800af607d5c36c8e78 to your computer and use it in GitHub Desktop.
Sometimes you just wanna plot something and not deal with matplotlib. I use this in logging messages sometimes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
#-*- coding: utf-8 -*- | |
import math | |
import random | |
import sys | |
from bisect import bisect | |
def printPlot(data, line=None, y=30, x=60, tick=.2, title="test", logger=None): | |
yax = "|" | |
xax = "_" | |
avbin = [] | |
scaledy = [] | |
lendata = len(data) | |
print(lendata) | |
ylab = str(max(data)) | |
sli = math.ceil(lendata / x) | |
for i in range(0, x): | |
avbin.append(int(sum(data[i * sli: (i + 1) * sli]) / sli )) | |
avmax = max(avbin) | |
print(avbin) | |
for j in avbin: | |
scaledy.append(int((j / avmax) * x)) | |
print(scaledy) | |
if line is not None: | |
scaled_line = int((line / avmax) * x) | |
lineidx = len(scaledy) - bisect(sorted(scaledy), scaled_line) | |
for idx, j in enumerate(scaledy): | |
if idx == 0: | |
sys.stdout.write(" " *int(x * .5) + title + "\n") | |
sys.stdout.write(" " * 9 + "0" + " " * (x - 4) + ylab + "\n") | |
sys.stdout.write(" " * 10 + yax + (xax * x) + "|" + "\n") | |
if line is not None: | |
if lineidx == idx: | |
sys.stdout.write(" " * 10 + yax + | |
"*" * (x - 4) + " " + str(line) + "\n") | |
if idx % int(tick * y) == 0: | |
# ticlab = str(len(data[0: len(data) - bisect(sorted(data), | |
# avbin[idx])])) | |
ticlab = str(int((j / x) * avmax)) | |
sys.stdout.write(ticlab.rjust(10, " " ) + yax + " " * j + "O\n") | |
else: | |
sys.stdout.write(" " * 10 + yax + " " * j + "O\n") | |
if __name__ == "__main__": | |
data = sorted(random.sample([x for x in range(0,200)] * 1000, 1000), reverse=True) | |
print(data) | |
printPlot(data=data, y=30, x=60, line = 75) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment