I've written a primitive Python script which "draws graphs" based on a list. It works, but it's a lot of lines for what I am trying to reach. The idea of the script is to function as a module that can be imported for a quick visual representation of data. Here's nextgraph.py:
from time import sleep
def draw_graph(
x,
y,
previous=None,
initial=None,
header_margin=1,
footer_margin=1,
anim_time=0.5,
graph_symbol="*",
indicator="|",
graph_subtitle="My graph"
):
if initial:
graph_ = initial
initial[x[y]][y] = graph_symbol
# This may be a bit confusing, in this case,
# x[y] means the current integer in a list (x)
# and not "x-coordinate[y-coordinate]"
else:
graph_ = previous
graph_[x[y]][y] = graph_symbol
# Same thing here
sleep(anim_time)
print(
"\n"*header_margin,
indicator.join(graph_[0]),
"\n", indicator.join(graph_[1]),
"\n", indicator.join(graph_[2]),
"\n", indicator.join(graph_[3]),
"\n", indicator.join(graph_[4]),
"\n", indicator.join(graph_[5]),
"\n", indicator.join(graph_[6]),
"\n", indicator.join(graph_[7]),
"\n", indicator.join(graph_[8]),
"\n", indicator.join(graph_[9]),
"\n", indicator.join(graph_[10]),
"\n", indicator.join(graph_[11]),
"\n", indicator.join(graph_[12]),
"\n", indicator.join(graph_[13]),
"\n", indicator.join(graph_[14]),
"\n", indicator.join(graph_[15]),
"\n", indicator.join(graph_[16]),
"\n", indicator.join(graph_[17]),
graph_subtitle,
"\033[00m",
"\n"*footer_margin)
return graph_
def update_graph(data,
top,
bottom,
sleep_,
color="\033[00m",
symbol="*",
seperator="|",
comment="My graph"
):
graph = [
[" ", " ", " ", " ", " ", " ", " ", " ", " ",
" ", " ", " ", " ", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " ", " ", " ",
" ", " ", " ", " ", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " ", " ", " ",
" ", " ", " ", " ", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " ", " ", " ",
" ", " ", " ", " ", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " ", " ", " ",
" ", " ", " ", " ", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " ", " ", " ",
" ", " ", " ", " ", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " ", " ", " ",
" ", " ", " ", " ", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " ", " ", " ",
" ", " ", " ", " ", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " ", " ", " ",
" ", " ", " ", " ", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " ", " ", " ",
" ", " ", " ", " ", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " ", " ", " ",
" ", " ", " ", " ", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " ", " ", " ",
" ", " ", " ", " ", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " ", " ", " ",
" ", " ", " ", " ", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " ", " ", " ",
" ", " ", " ", " ", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " ", " ", " ",
" ", " ", " ", " ", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " ", " ", " ",
" ", " ", " ", " ", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " ", " ", " ",
" ", " ", " ", " ", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " ", " ", " ",
" ", " ", " ", " ", " ", " ", " ", " ", " "]
]
markers = 0
while markers != 17:
print("\033[00m")
for _ in data:
print(color)
if markers == 0:
old = draw_graph(data,
markers,
initial=graph,
header_margin=top,
footer_margin=bottom,
anim_time=sleep_,
graph_symbol=symbol,
indicator=seperator,
graph_subtitle=comment
)
else:
old = draw_graph(data,
markers,
old,
header_margin=top,
footer_margin=bottom,
anim_time=sleep_,
graph_symbol=symbol,
indicator=seperator,
graph_subtitle=comment
)
markers += 1
print("\033[00m")
And here's a sample implementation (GraphTest.py):
from nextgraph import update_graph
from time import time
stream = [
[3,3,3,3,4,4,4,3,3,3,3,4,3,2,1,2,2],
[2,3,4,5,5,6,5,6,5,6,6,7,8,8,9,10,9],
[10,11,12,13,13,14,14,15,14,14,13,12,11,10,9,8,7],
[7,7,7,8,8,7,7,7,6,5,5,4,5,3,1,2,2],
[3,3,4,3,4,3,4,5,6,6,5,4,3,2,1,1,2]
]
count = 0
for substream in stream:
update_graph(data=substream,
top=30,
bottom=8,
sleep_=0.2
)
count += 1
I am having trouble getting this to be more scalable. Adding a row/column would currently require adding another two lines to the code, so for a larger number (128 / 256 positions), this would become unmanageable. Also, for each added row, the print() function in draw_graph needs another line. If anybody has any suggestions on how to simplify the graph matrix or draw_graph / update_graph functions, or otherwise on how to improve the code, I'd love to hear.