Skip to content

Instantly share code, notes, and snippets.

@turnipsoup
Last active January 28, 2020 17:03
Show Gist options
  • Save turnipsoup/cdd2a628800f621cf43872cf2c95afa5 to your computer and use it in GitHub Desktop.
Save turnipsoup/cdd2a628800f621cf43872cf2c95afa5 to your computer and use it in GitHub Desktop.
A thin wrapper to make Seaborn charts from the command line.
#!/usr/bin/env python3
#
# Here we parse the arguments and check their validity BEFORE importing the heavy libraries, just in case. Saves CPU.
# Arguments parsing
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-f", help="File name")
parser.add_argument("-c", help="Chart type. To view list of support charts, use --charts")
parser.add_argument("-x", help="X column to be passed with chart type")
parser.add_argument("-y", help="Y column to be passed with chart type")
parser.add_argument("--hue", help="Will color the chart based on this variable, if supported.")
parser.add_argument("--charts", help="List all available chart types - only pass this", action="store_true")
parser.add_argument("--color", help="Color scheme of the chart, if supported. Takes a hex code without leading #")
parser.add_argument("--theme", help="A seaborn theme")
args=parser.parse_args()
# So we only have to configure this once
def chartsShow():
print("Current types supported:")
print(" [type] <desc> (flags, [optional-flags])")
print(" -")
print(" [dist] Distribution plot (data, X)")
print(" [bar] Bar plot (data, X, Y, [hue])")
print(" [scatter] Scatter plot (data, X, Y, [hue])")
print(" [hex] Hexagon joint plot (data, X, Y, [color])")
# Flag preflight check
if args.charts:
chartsShow()
exit(0)
if not args.f:
print("You need to include a filename")
exit(1)
if not args.c:
print("You must include a chart type")
chartsShow()
exit(1)
# Imports AFTER flag checks, to save CPU
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
import datetime
# Set the theme if passed
if args.theme:
sns.set_style(args.theme)
else:
sns.set()
# Load CSV into Pandas
try:
df = pd.read_csv(args.f)
except:
print("Either your file doesn't exist or is not a real CSV. Please pass CSVs only.")
exit(1)
# Arguments and Chart Building
if args.c == "dist":
sns.distplot(df[args.x])
figure_file_name = "dist-" + str(datetime.datetime.now()).replace(" ", "-") + ".png"
plt.savefig(figure_file_name)
print(f"Chart saved to ./{figure_file_name}")
exit(0)
if args.c == "bar":
if args.hue:
sns.barplot(data=df, x=args.x, y=args.y, hue=args.hue)
figure_file_name = "bar-" + str(datetime.datetime.now()).replace(" ", "-") + ".png"
plt.savefig(figure_file_name)
print(f"Chart saved to ./{figure_file_name}")
exit(0)
else:
sns.barplot(data=df, x=args.x, y=args.y)
figure_file_name = "bar-" + str(datetime.datetime.now()).replace(" ", "-") + ".png"
plt.savefig(figure_file_name)
print(f"Chart saved to ./{figure_file_name}")
exit(0)
if args.c == "scatter":
if args.hue:
sns.scatterplot(data=df, x=args.x, y=args.y, hue=args.hue)
figure_file_name = "scatter-" + str(datetime.datetime.now()).replace(" ", "-") + ".png"
plt.savefig(figure_file_name)
print(f"Chart saved to ./{figure_file_name}")
exit(0)
else:
sns.scatterplot(data=df, x=args.x, y=args.y)
figure_file_name = "scatter-" + str(datetime.datetime.now()).replace(" ", "-") + ".png"
plt.savefig(figure_file_name)
print(f"Chart saved to ./{figure_file_name}")
exit(0)
if args.c == "hex":
if args.color:
sns.jointplot(data=df, x=args.x, y=args.y, color=f"#{args.color}", kind='hex')
figure_file_name = "scatter-" + str(datetime.datetime.now()).replace(" ", "-") + ".png"
plt.savefig(figure_file_name)
print(f"Chart saved to ./{figure_file_name}")
exit(0)
else:
sns.jointplot(data=df, x=args.x, y=args.y, kind='hex')
figure_file_name = "scatter-" + str(datetime.datetime.now()).replace(" ", "-") + ".png"
plt.savefig(figure_file_name)
print(f"Chart saved to ./{figure_file_name}")
exit(0)
@turnipsoup
Copy link
Author

turnipsoup commented Jan 27, 2020

Run ./chartmake.py --help.

Requirements: pip3 install pandas seaborn numpy matplotlib

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment