Skip to content

Instantly share code, notes, and snippets.

@tkw1536
Created March 9, 2014 16:58
Show Gist options
  • Save tkw1536/9450760 to your computer and use it in GitHub Desktop.
Save tkw1536/9450760 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ipython
#
# Plots X/Y/T Data from a csv file
# (c) Tom Wiesing 2014
#
from numpy import *
from scipy import *
from pylab import *
import sys
import argparse
import matplotlib.pyplot as plt
def do_plot(x, y):
plt.plot(x, y, ".")
def lshow(title, xlabel, ylabel, out):
plt.xlabel(xlabel)
plt.ylabel(ylabel)
plt.title(title)
if out == None:
plt.show()
else:
plt.savefig(out)
def get_csv_data(filename, opts):
(h, f, t, x, y, d) = opts
data = genfromtxt(filename, delimiter=d, skip_header=h,
skip_footer=f)
return (data[:,t], data[:,x], data[:,y])
def plot_csv_x_y(filename, o, xlabel="", ylabel="", title="", out=None):
(t, x, y) = get_csv_data(filename, o)
do_plot(x, y)
lshow(title, xlabel, ylabel, out)
def plot_csv_x_t(filename, o, xlabel="", ylabel="", title="", out=None):
(t, x, y) = get_csv_data(filename, o)
do_plot(t, x)
lshow(title, xlabel, ylabel, out)
def plot_csv_y_t(filename, o, xlabel="", ylabel="", title="", out=None):
(t, x, y) = get_csv_data(filename, o)
do_plot(t, y)
lshow(title, xlabel, ylabel, out)
def plot_csv_x_y_t(filename, o, xlabel="", ylabel="", title="", out=None):
(t, x, y) = get_csv_data(filename, o)
do_plot(t, x)
do_plot(t, y)
lshow(title, xlabel, ylabel, out)
def main(args):
parser = argparse.ArgumentParser(description='Quick X-Y-T plotter for CSV file data')
whattoplot = parser.add_argument_group("What to plot").add_mutually_exclusive_group()
whattoplot.add_argument("-x", "--x-versus-t", dest="type", action="store_const", const="x", default="xy", help="Plot x versus t")
whattoplot.add_argument("-y", "--y-versus-t", dest="type", action="store_const", const="y", help="Plot y versus t")
whattoplot.add_argument("-xyt", "--xy-versus-t", dest="type", action="store_const", const="xyt", help="Plot x and y versus t")
whattoplot.add_argument("-xy", "--x-versus-y", dest="type", action="store_const", const="xy", help="Plot x vsersus y")
parser.add_argument('filename', help='Which file to plot')
parser.add_argument('--title', default="", help='Title for plot. ')
parser.add_argument('--xlabel', default="", help='X Label for plot. ')
parser.add_argument('--ylabel', default="", help='Y Label for plot. ')
parser.add_argument('-o', '--output', default=None, help='Save figure to file. ')
dataopts = parser.add_argument_group("CSV parsing options")
parser.add_argument('--header-size', '-hs', default=5, type=int, help='Header size of CSV files. Number of lines to ignore at the start of the CSV file. ')
parser.add_argument('--footer-size', '-fs', default=1, type=int, help='Footer size of CSV files. Number of lines to ignore at the end of the CSV file. ')
parser.add_argument('--t-column', '-tc', default=1, type=int, help='Column to use for x values. ')
parser.add_argument('--x-column', '-xc', default=2, type=int, help='Column to use for x values. ')
parser.add_argument('--y-column', '-yc', default=3, type=int, help='Column to use for x values. ')
parser.add_argument('--delimiter', '-d', default=",", help='Delimiter to use for csv files. ')
args = parser.parse_args()
csvopts = (args.header_size, args.footer_size, args.t_column, args.x_column, args.y_column, args.delimiter)
if args.type == "x":
plot_csv_x_t(args.filename, csvopts, title=args.title, xlabel=args.xlabel, ylabel=args.ylabel, out=args.output)
if args.type == "y":
plot_csv_y_t(args.filename, csvopts, title=args.title, xlabel=args.xlabel, ylabel=args.ylabel, out=args.output)
if args.type == "xy":
plot_csv_x_y(args.filename, csvopts, title=args.title, xlabel=args.xlabel, ylabel=args.ylabel, out=args.output)
if args.type == "xyt":
plot_csv_x_y_t(args.filename, title=args.title, xlabel=args.xlabel, ylabel=args.ylabel, out=args.output)
if __name__ == "__main__":
main(sys.argv)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment