trigplot-h.py, fixed

#!/usr/bin/env python3
# 2021-04-12
import math
import shutil

def myftn(x, scale):
    return (math.sin(x) * math.cos(scale * x))
#--------

def plot_function(xvalues, yvalues, fill=' '):
    width = shutil.get_terminal_size()[0] - 8
    height = shutil.get_terminal_size()[1] - 2

    nsteps = len(xvalues)
    for n in range(nsteps):
        line = '{:5.2f}: '.format(xvalues[n]) \
            + fill * int(width*(yvalues[n]+1)/2) \
            + '{:.2f}'.format(yvalues[n])
        print(line)
#--------

def plot_horizontal(xvalues, yvalues, fill=' '):
    n_cols = shutil.get_terminal_size()[0] - 6
    n_rows = shutil.get_terminal_size()[1] - 4
    print('{:d} columns, {:d} rows'.format(n_cols, n_rows))

    xmin, xmax = min(xvalues), max(xvalues)
    xrange = xmax - xmin
    #cols =[]
    rows =[]
    #for i, x in enumerate(xvalues):
    #    y = yvalues[i]
    #    x_c = width * x/xmax
    #    for c in range(0, width):
    #        if c <= x_c < (c+1):
    #            #cols.append(x)
    #            rows.append(y)
    for c in range(n_cols):
        x_c = xmin +xrange * c/n_cols
        for i,x in enumerate(xvalues):
            if x_c <= x:
                rows.append(yvalues[i])
                break

    ymax, ymin = max(rows), min(rows)
    print(ymax, ymin)
    for r in range(n_rows, 0, -1):
        y_scaled = ymin + (ymax - ymin) * r/n_rows
        y_next = ymin + (ymax - ymin) * (r-1)/n_rows

        #print('{:5.2f}'.format(y_scaled), end='|')
        rowlist = ['{:5.2f}|'.format(y_scaled)]
        for c in range(n_cols):
            if y_scaled >= rows[c] >= y_next or y_scaled < rows[c] <= y_next:
                rowlist.append('*')
            elif rows[c] > y_scaled >= 0 or rows[c] <= y_scaled < 0:
                rowlist.append('|')
            elif y_scaled > 0 >= y_next:
                rowlist.append('_')
            else:
                rowlist.append(' ')
        print(''.join(rowlist))
#--------               

def main(argv=[__name__]):
    if len(argv) == 3:
        xrange = float(argv[1])
        scale = float(argv[2])
    else:
        xrange = 6
        scale = float(input('Scale? '))

    x = 1e-99
    xmax = xrange * math.pi
    nsteps = 200
    xstep = (xmax - x) / nsteps
    xvalues, yvalues = [], []

    while x < xmax:
        xvalues.append(x)
        yvalues.append(myftn(x, scale))
        x += xstep

    #plot_function(xvalues, yvalues, fill='*')    
    plot_horizontal(xvalues, yvalues, fill='*')
#--------

if __name__ == '__main__':
    import sys
    sys.exit(main(sys.argv))
#--------