Last active
December 9, 2015 03:52
-
-
Save emgram769/9e93cbfbfd95b36839ea to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| import subprocess | |
| def get_terminal_size(): | |
| import os | |
| env = os.environ | |
| def ioctl_GWINSZ(fd): | |
| try: | |
| import fcntl, termios, struct, os | |
| cr = struct.unpack('hh', fcntl.ioctl(fd, termios.TIOCGWINSZ, | |
| '1234')) | |
| except: | |
| return | |
| return cr | |
| cr = ioctl_GWINSZ(0) or ioctl_GWINSZ(1) or ioctl_GWINSZ(2) | |
| if not cr: | |
| try: | |
| fd = os.open(os.ctermid(), os.O_RDONLY) | |
| cr = ioctl_GWINSZ(fd) | |
| os.close(fd) | |
| except: | |
| pass | |
| if not cr: | |
| cr = (env.get('LINES', 25), env.get('COLUMNS', 80)) | |
| return int(cr[1]), int(cr[0]) | |
| def color_str(string, color_num): | |
| return '\x1b[3%dm%s\x1b[0m' % (color_num, string) | |
| def colorify(to_color): | |
| line_styles = ['*', '#'] | |
| for i,style in enumerate(line_styles): | |
| to_color = to_color.replace(style, color_str(style, i+3)) | |
| return to_color | |
| class Plot(object): | |
| def __init__(self): | |
| self.plots = [] | |
| def add_data(self,x,y,title=""): | |
| self.plots.append({ "x": x, "y": y, "title": title }) | |
| # Usage | |
| # width: width of the output | |
| # height: height of the output | |
| def scatter_plot(self, width=-1, height=-1): | |
| plots = self.plots | |
| if width == -1 or height == -1: | |
| width, height = get_terminal_size() | |
| gnuplot = subprocess.Popen(["/usr/bin/gnuplot"], | |
| stdin=subprocess.PIPE, | |
| stdout=subprocess.PIPE) | |
| stdinput = [] | |
| stdinput.append(("set term dumb %d %d\n"%(width, height))) | |
| stdinput.append(("set datafile separator ','\n")) | |
| # Create the lines and titles | |
| stdinput.append("plot ") | |
| for (i,plot) in enumerate(plots): | |
| title = plot["title"] if plot["title"] else "Line %d" % (i) | |
| stdinput.append(("'-' using 1:2 title '%s' with lines, " % (title))) | |
| stdinput.append("\n") | |
| # Plot the lines | |
| for plot in plots: | |
| for i,j in zip(plot["x"],plot["y"]): | |
| stdinput.append(("%f,%f\n" % (i,j))) | |
| stdinput.append("e\n") | |
| #gnuplot.stdin.flush() | |
| out, err = gnuplot.communicate(input=''.join(stdinput).encode('utf-8')) | |
| print(colorify(out.decode('utf-8'))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment