Instantly share code, notes, and snippets.

Embed
What would you like to do?
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