def plot_scatter(filename=None, xs=None, ys=None, size=20, pch='x', colour='white', title=None, skipheader=False): """plot (X,y) pairs from columns in a file or 2 separate files Arguments: xs List of values to use on X axis [default: None] ys List of values to use on y axis [default: None] filename Name of a csv file [default: None] skipheader Skip the first row [default: False] size Marketsize [default: 20] pch Markershape [default: x] colour Marker colour [default: white] title Title [default: ] """ if filename: data = helpers.read_xy_pairs_from_csv(filename, X=0, y=1, header=skipheader) xs = [i[0] for i in data] ys = [i[1] for i in data] else: xs = [line.strip() for line in open(xs, 'r').readlines()] ys = [line.strip() for line in open(ys, 'r').readlines()] if skipheader: xs = xs[1:] ys = ys[1:] xs = map(float, xs) ys = map(float, ys) if (xs is None or ys is None): raise Exception("Missing `xs` or `ys` data.") plotted = set() if title: print(helpers.box_text(title, 2 * len(get_scale(xs, False, size)) + 1)) print("-" * (2 * len(get_scale(xs, False, size)) + 2)) for y in get_scale(ys, True, size): print("|", end=' ') for x in get_scale(xs, False, size): point = " " for (i, (xp, yp)) in enumerate(zip(xs, ys)): if xp <= x and yp >= y and (xp, yp) not in plotted: point = pch #point = str(i) plotted.add((xp, yp)) if x == 0 and y == 0: point = "o" elif x == 0: point = "|" elif y == 0: point = "-" helpers.printcolour(point, True, colour) print("|") print("-" * (2 * len(get_scale(xs, False, size)) + 2))
def plot_hist(data, height=20.0, bincount=None, pch="o", colour="white", title="", xlab=None, showSummary=False): """make a histogram for continuous variable. Arguments: data: List of numbers or file with numbers height: The height of the histogram in # of lines bincount: Number of bins in the histogram pch: Shape of the bars in the plot colour: Colour of the bars in the terminal title: Title at the top of the plot xlab: Boolen value for whether or not to display x-axis labels showSummary: Boolean value for whether or not to display a summary """ if pch is None: pch = "o" colour = helpers.get_colour(colour) min_val, max_val = None, None n, mean = 0., 0. for number in read_numbers(data): n += 1 if not min_val or number < min_val: min_val = number if not max_val or number > max_val: max_val = number mean += number mean /= n bins = list(calc_bins(n, min_val, max_val, bincount)) hist = {} for i in range(len(bins)): hist[i] = 0 for number in read_numbers(data): for i, b in enumerate(bins): if number < b: hist[i] += 1 break min_y, max_y = min(hist.values()), max(hist.values()) ys = list(helpers.drange(min_y, max_y, (max_y-min_y)/height)) ys.reverse() nlen = max(len(str(min_y)), len(str(max_y))) + 1 if title: print helpers.box_text(title, len(hist)*2, nlen) print used_labs = set() for y in ys: ylab = str(int(y)) if ylab in used_labs: ylab = "" else: used_labs.add(ylab) ylab = " "*(nlen - len(ylab)) + ylab + "|" print ylab, for i in range(len(hist)): if y < hist[i]: helpers.printcolor(pch, True, colour) else: helpers.printcolor(" ", True, colour) print xs = hist.keys() * 2 print " "*(nlen+1) + "-"*len(xs) if xlab: for i in range(0, nlen): helpers.printcolor(" "*(nlen+1), True, colour) for x in range(0, len(hist)): num = str(bins[x]) if x%2==0: print " ", elif i < len(num): print num[i], print center = max(map(len, map(str, [n, min_val, mean, max_val]))) center += 15 if showSummary: print print "-"*(2 + center) print "|" + "Summary".center(center) + "|" print "-"*(2 + center) summary = "|" + ("observations: %d" % n).center(center) + "|\n" summary += "|" + ("min value: %f" % min_val).center(center) + "|\n" summary += "|" + ("mean : %f" % mean).center(center) + "|\n" summary += "|" + ("max value: %f" % max_val).center(center) + "|\n" summary += "-"*(2 + center) print summary