2
0
Fork 0
mirror of https://github.com/MartinThoma/LaTeX-examples.git synced 2025-04-26 06:48:04 +02:00

added and automated beautification done by jenson_bo

This commit is contained in:
Martin Thoma 2014-08-06 11:18:27 -04:00
parent 0e289dd133
commit 38478cc7e3
4 changed files with 78 additions and 25 deletions

View file

@ -4,6 +4,7 @@
from __future__ import print_function
import shutil
import fileinput
import math
def main(filename, bins, maximum):
@ -17,7 +18,7 @@ def main(filename, bins, maximum):
numbers = sorted(numbers)
minimum = min(numbers)
bin_counter = [0 for i in range(bins+1)]
borders = []
xticklabels = []
for i, number in enumerate(numbers):
if number >= minimum + (maximum - minimum)/bins*(bins+1):
bin_counter[bins] += 1
@ -32,21 +33,56 @@ def main(filename, bins, maximum):
break
for b in range(bins):
lower = minimum + (maximum - minimum)/bins*b
borders.append(str(lower))
borders.append("\infty")
return bin_counter, borders
xticklabels.append(get_xticklabel(lower))
# Get labels for y-axis
ytickslabels = []
maxylabel = int(10**math.floor(math.log(max(bin_counter), 10)))
ylabelsteps = maxylabel / 10
for i in range(0, maxylabel+1, ylabelsteps):
ytickslabels.append(get_yticklabel(i, True))
xticklabels.append("\infty")
return bin_counter, xticklabels, ytickslabels
def modify_template(bin_counter, borders):
def get_xticklabel(value):
return str(int(value/1000))
def get_yticklabel(value, si_suffix):
if si_suffix:
divide_by, suffix = get_si_suffix(value)
new_value = (value / divide_by)
if int(new_value) == new_value:
return ("%i" % int(new_value)) + suffix
else:
return ("%0.2f" % new_value) + suffix
else:
return str(value)
def get_si_suffix(value):
if value >= 10**3:
return (10**3, "K")
elif value >= 10**6:
return (10**6, "M")
else:
return (1, "")
def modify_template(bin_counter, xticklabels, yticklabels):
shutil.copyfile("histogram-large-1d-dataset.template.tex",
"histogram-large-1d-dataset.tex")
xticklabels = ", ".join(map(lambda n: "$%s$" % n, borders))
xticklabels = ", ".join(map(lambda n: "$%s$" % n, xticklabels))
yticklabels = ", ".join(yticklabels)
coordinates = ""
for i, value in enumerate(bin_counter):
coordinates += "(%i, %i) " % (i, value)
for line in fileinput.input("histogram-large-1d-dataset.tex",
inplace=True):
line = line.replace("{{xticklabels}}", xticklabels)
line = line.replace("{{yticklabels}}", yticklabels)
line = line.replace("{{coordinates}}", coordinates)
print(line, end='')
@ -65,5 +101,7 @@ if __name__ == '__main__':
help=("what is the maximum number "
"that should get binned?"))
args = parser.parse_args()
bin_counter, borders = main(args.filename, args.bins, args.max)
modify_template(bin_counter, borders)
bin_counter, xticklabels, yticklabels = main(args.filename,
args.bins,
args.max)
modify_template(bin_counter, xticklabels, yticklabels)