0
\$\begingroup\$

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.

\$\endgroup\$
3
\$\begingroup\$
  • You may want to change update_graph so that it uses *args and **kwargs. update_graph(data, *args, color="\033[00m", **kwargs). This would allow you to then call draw_graph with less arguments, all on the same line:

    if markers == 0:
        old = draw_graph(data, markers, *args, initial=graph, **kwargs)
    else:
        old = draw_graph(data, markers, *args, previous=old, **kwargs)
    
  • Rather than manually building the graph, you can instead use a comprehension and *. graph = [[" "] * 18 for _ in range(18)]. This is much more readable, and takes up much less space.

  • Rather than manually joining all the lines in the graph, you could instead use join each line with " \n " as that's what , "\n", is doing. This assumes that graph_ is only 18 lines high.

    " \n ".join(indicator.join(g) for g in graph_)
    
  • You could probably also use use iter and next, rather than using markers == 0, however that's may be less readable to you.

  • Rather than using graph_ = initial and graph_ = previous you could just pass graph_ this would remove the if's in both update_graph and draw_graph. As they're doing the same thing.

  • You may want to create an enum of colours, rather than using magic strings, such as "\033[00m".

\$\endgroup\$
  • \$\begingroup\$ Thanks for your answers, I'm currently implementing them, but I'm stuck with a question regarding your answer. You mention I should pass graph_. I'm not sure what you mean by that. Does this mean I call my old variable graph_, and then pass it to update_graph? \$\endgroup\$ – Daniel May 28 '17 at 15:13
  • 1
    \$\begingroup\$ @Coal_ Sorry I went out, yeah, remove initial and previous, and instead use graph_. \$\endgroup\$ – Peilonrayz May 28 '17 at 18:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.