Skip to content

Instantly share code, notes, and snippets.

@ctindall
Created March 26, 2018 18:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ctindall/12f61387efb892056594e0b91585b5ac to your computer and use it in GitHub Desktop.
Save ctindall/12f61387efb892056594e0b91585b5ac to your computer and use it in GitHub Desktop.
convert an org-mode file with a table of data into a handsome graph
#!/usr/bin/env bash
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
function trampcp {
src="$1"
dest="$2"
emacs -q \
--batch \
--eval "(copy-file \"$src\" \"$dest\" t)"
}
function org2csv {
src="$1"
dest="$2"
emacs -q \
--batch \
--file "$src" \
--eval "(org-table-export \"$dest\" \"orgtbl-to-csv\")"
sed -i 's/^\[//' $dest
sed -i 's/\ [A-Z][a-z][a-z]\]//' $dest
}
function csv2png {
#usage: csv2png SRC DEST
python3 -c "
import sys
import pandas as pd
import matplotlib
#use a noninteractive backend (this must happen before we import pyplot)
matplotlib.use('agg')
import matplotlib.pyplot as plt
src=sys.argv[1]
dest=sys.argv[2]
data = pd.read_csv(src, header=0)
data['date'] = pd.to_datetime(data['date'])
data = data.set_index('date')
data.resample('M')
for col in data.columns:
plt.plot(data.index, data[col], '-', label=col)
plt.legend()
plt.grid(True)
matplotlib.pyplot.savefig(dest, bbox='tight', pad_inches=2)
" $@
}
function org2graph {
orgfile="$1"
csvfile=$(mktemp).csv
pngfile=$(mktemp).png
destfile="$2"
org2csv "$orgfile" "$csvfile"
csv2png "$csvfile" "$pngfile"
trampcp "$pngfile" "$destfile"
rm "$csvfile" "$pngfile"
}
org2graph "$1" "$2"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment