Skip to content

Instantly share code, notes, and snippets.

@jolynch
Created August 18, 2017 18:31
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 jolynch/0abc6bfadc7996d4b18f3d74b69d80b4 to your computer and use it in GitHub Desktop.
Save jolynch/0abc6bfadc7996d4b18f3d74b69d80b4 to your computer and use it in GitHub Desktop.
Making Pretty Graphs
#!/usr/bin/env bash
# Utility for compiling gnuplot scripts
# to pdf and png images containing latex text
#
# requires: awk, latex, dvips, convert, gnuplot
# convert is a part of "Imagemagick" suite for image manipulation
#
# usage: figlatex script.gp (format)
#
# script.gp should contain an
# output file with tex extension, e.g., 'output.tex'
# and use epslatex terminal
#
#
# Coded by Marko Budisic
# mbudisic@gmail.com
# 2009-02-27
#
# This work is licensed under the Creative Commons Attribution 3.0
# Unported License. To view a copy of this license, visit
# http://creativecommons.org/licenses/by/3.0/ or send a letter to
# Creative Commons, 171 Second Street, Suite 300, San Francisco,
# California, 94105, USA. Please provide a link back to this source code,
# and/or mention the original author if you reuse or derive from this
# work. Thank you.
# fail on error
set -e
if [ -z $1 ];
then
echo "usage: figlatex SCRIPT (OUTPUT)"
echo "where OUTPUT is either hq, ps, tif, or png (default if argument omitted)"
exit -1;
fi
if [ ! -f $1 ];
then
echo "Argument has to be a file. What isn't a gnuplot script: $1"
exit -1;
fi
pserase=1
# output format
if [ ! -z $2 ];
then
outformat=$2
else
outformat="png"
fi
case $outformat in
("hq") echo "Output is Hi-Q PNG (600dpi)";;
("ps") echo "Output is PS";;
("tif") echo "Output is TIF";;
(*) echo "Output is PNG";;
esac
# retrieve the lowest-level of my current path
# parsing syntax based on Janis Papanagnou's forum post on 2007-03-24
myloc=`pwd`
myloc=${myloc%/};
myloc=${myloc##*/}/;
echo "***********"
echo " awk -> get tex filename from $myloc$1"
# use awk to find the line containing "set output" gnuplot command
# split it into fields (set, output, filename)
# split filename at the dot
# take the part before the dot, and strip first character
# if there are more lines that contain "output", it will retain the result of the last one
fcore="`awk '/set output/ {split($3,tok,DOT); z=substr(tok[1],2); }; END {print(z);}' DOT="." $1`"
echo "***********"
echo " gnuplot -> compile $myloc$1"
gnuplot $1
echo "***********"
echo " latex -> $myloc$fcore.tex to $myloc$fcore.dvi"
latex -interaction=batchmode $fcore.tex
#latex $fcore.tex
echo "***********"
echo " dvips -> compile $myloc$fcore.dvi to $myloc$fcore.ps"
dvips -o $fcore.ps $fcore.dvi
case $outformat in
("ps") ;;
("hqpng")
echo "***********"
echo " convert -> convert $myloc$fcore.ps to $myloc$fcore.png"
convert -density 500 $fcore.ps +trim +repage \
-flatten -background white -depth 8 -bordercolor \
white -border 15 -quality 100 $fcore.png
rm ${fcore}.ps
;;
("tif")
echo "***********"
echo " convert -> convert $myloc$fcore.ps to $myloc$fcore.tif"
convert -density 500 $fcore.ps +trim +repage \
-flatten -background white -depth 8 -bordercolor \
white -border 15 -compress lzw $fcore.tif
rm ${fcore}.ps
;;
(*)
echo "***********"
echo " convert -> convert $myloc$fcore.ps to $myloc$fcore.png"
convert -density 300 $fcore.ps +trim +repage \
-flatten -background white -depth 8 -bordercolor \
white -border 15 -quality 100 $fcore.png
rm ${fcore}.ps
;;
esac
echo "***********"
echo " cleanup of $myloc$fcore"
# removes files left by latex and image files generated in the process
rm $fcore{.dvi,-inc.eps,.log,.aux,.tex}
# Generate the data file
ab -g control.dat -c 4 -n 100 'http://127.0.0.1:1234/control'
# Generate the outputs
./figlatex.sh nginx_overlay.plt
# Let's output to a png file
set terminal epslatex color colortext standalone header '\usepackage{color}'
set output 'nginx_overlay.tex'
# The graph title
set title "Figure 7: Overlay Latency (Control + NGINX Reloads)"
# Where to place the legend/key
# set key left top
unset key
# Draw gridlines oriented on the y axis
set grid y
#set logscale y
set yrange [0:20]
# Specify that the x-series data is time data
set xdata time
# Specify the *input* format of the time data
set timefmt "%s"
# Specify the *output* format for the x-axis tick labels
set format x "%S"
# Label the x-axis
set xlabel 'Time'
# Label the y-axis
set ylabel "Sample Response Time (ms)"
# Tell gnuplot to use tabs as the delimiter instead of spaces (default)
set datafile separator '\t'
# Plot the data
#set style line 5 lt rgb "green" lw 3 pt 6
#set style line 4 lt rgb "red" lw 3 pt 3
set format x ""
# Plot the data files you generated with ab
plot "control.dat" every ::2 using 2:5 title 'control response time' with points pointtype 4 lt rgb "green"
# Could plot more here
# plot "nginx_reload.dat" every ::2 using 2:5 title 'nginx reload response time' with points pointtype 2 ps 0.6 lt rgb "blue"
# plot "nginx_sighup.dat" every ::2 using 2:5 title 'reload response time' with points pointtype 1 ps 0.6 lt rgb "purple"
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment