Created
October 17, 2018 12:04
-
-
Save vilkoz/5e7d6092cd09ea4ecd66b5a4e2a93eda to your computer and use it in GitHub Desktop.
draw simple dot plot in terminal
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 | |
def generate_empty_map(x, y): | |
m = [] | |
for _ in range(y): | |
row = [] | |
for __ in range(x): | |
row.append(' ') | |
m.append(row) | |
return m | |
def interpolate_value(val, _from, _to): | |
val = ((val - _from[0]) / (_from[1] - _from[0])) | |
val = (val * (_to[1] - _to[0])) + _to[0] | |
return val | |
def clamp(val, _from, _to): | |
if _from > _to: | |
print("_from > _to what?") | |
if val < _from: | |
return _from | |
if val > _to: | |
return _to | |
return val | |
def output_map(m): | |
for line in reversed(m): | |
for c in line: | |
print(c, end="") | |
print("") | |
def draw_array(x, y): | |
if len(x) != len(y): | |
print("different input array dimensions") | |
return | |
h = 25 | |
w = 80 | |
m = generate_empty_map(w, h) | |
data_limits = { | |
"x": (min(x), max(x)), | |
"y": (min(y), max(y)) | |
} | |
data_dimension = len(x) | |
delta_x = h / data_dimension | |
for i, x_val in enumerate(x): | |
x_coord = int(interpolate_value(x[i], data_limits['x'], (0, w-1))) | |
y_coord = int(interpolate_value(y[i], data_limits['y'], (0, h-1))) | |
m[clamp(y_coord, 0, h-1)][clamp(x_coord, 0, w-1)] = 'x' | |
output_map(m) | |
def main(): | |
x = [64, 128, 256, 512, 1024, 2024] | |
y = [700, 760, 1300, 2200, 5700, 12000] | |
draw_array(x, y) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment