2
0
Fork 0
mirror of https://github.com/MartinThoma/LaTeX-examples.git synced 2025-04-19 11:38:05 +02:00

added example how to easily create a histogramm from much data

This commit is contained in:
Martin Thoma 2014-08-05 15:39:13 -04:00
parent 18ab17e5a9
commit 98d00821e9
6 changed files with 167049 additions and 0 deletions

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,35 @@
SOURCE = histogram-large-1d-dataset
DELAY = 80
DENSITY = 300
WIDTH = 512
make:
pdflatex $(SOURCE).tex -output-format=pdf
make clean
clean:
rm -rf $(TARGET) *.class *.html *.log *.aux *.data *.gnuplot
gif:
pdfcrop $(SOURCE).pdf
convert -verbose -delay $(DELAY) -loop 0 -density $(DENSITY) $(SOURCE)-crop.pdf $(SOURCE).gif
make clean
png:
make
make svg
inkscape $(SOURCE).svg -w $(WIDTH) --export-png=$(SOURCE).png
transparentGif:
convert $(SOURCE).pdf -transparent white result.gif
make clean
svg:
make
#inkscape $(SOURCE).pdf --export-plain-svg=$(SOURCE).svg
pdf2svg $(SOURCE).pdf $(SOURCE).svg
# Necessary, as pdf2svg does not always create valid svgs:
inkscape $(SOURCE).svg --export-plain-svg=$(SOURCE).svg
rsvg-convert -a -w $(WIDTH) -f svg $(SOURCE).svg -o $(SOURCE)2.svg
inkscape $(SOURCE)2.svg --export-plain-svg=$(SOURCE).svg
rm $(SOURCE)2.svg

View file

@ -0,0 +1,3 @@
Compiled example
----------------
![Example](histogram-large-1d-dataset.png)

Binary file not shown.

After

Width:  |  Height:  |  Size: 29 KiB

View file

@ -0,0 +1,44 @@
\documentclass[margin=10pt]{standalone}
\usepackage{amsmath}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
\newcommand\clipright[1][white]{
\fill[#1](current axis.south east)rectangle(current axis.north-|current axis.outer east);
\pgfresetboundingbox
\useasboundingbox(current axis.outer south west)rectangle([xshift=.5ex]current axis.outer north-|current axis.east);
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[/tikz/ybar interval,
ybar legend,
xtick align=outside,
ymin=0,
axis x line*=left,
enlarge x limits=false,
grid=major,
height=7cm,
title={All Results},
xlabel={recording time $t$ in ms},
ylabel={Number of Recordings},
xtick={0,...,16},
xticklabels={ {{xticklabels}} },
width=\textwidth,
xtick=data,
xticklabel style={
inner sep=0pt,
anchor=north east,
rotate=45
},
nodes near coords,
every node near coord/.append style={
anchor=mid west,
rotate=45},
]
\addplot[blue, fill=blue!40!white] coordinates { {{coordinates}} };
\legend{Time}
\end{axis}
\clipright
\end{tikzpicture}
\end{document}

View file

@ -0,0 +1,69 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function
import shutil
import fileinput
def main(filename, bins, maximum):
with open(filename) as f:
content = f.read().split("\n")
numbers = []
for line in content:
line = line.strip()
if line != "":
numbers.append(float(line))
numbers = sorted(numbers)
minimum = min(numbers)
bin_counter = [0 for i in range(bins+1)]
borders = []
for i, number in enumerate(numbers):
if number >= minimum + (maximum - minimum)/bins*(bins+1):
bin_counter[bins] += 1
elif number < minimum:
bin_counter[0] += 1
else:
for b in range(bins):
lower = minimum + (maximum - minimum)/bins*b
upper = minimum + (maximum - minimum)/bins*(b+1)
if lower <= number < upper:
bin_counter[b] += 1
break
for b in range(bins):
lower = minimum + (maximum - minimum)/bins*b
borders.append(str(lower))
borders.append("\infty")
return bin_counter, borders
def modify_template(bin_counter, borders):
shutil.copyfile("histogram-large-1d-dataset.template.tex",
"histogram-large-1d-dataset.tex")
xticklabels = ", ".join(map(lambda n: "$%s$" % n, borders))
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("{{coordinates}}", coordinates)
print(line, end='')
if __name__ == '__main__':
from argparse import ArgumentParser
parser = ArgumentParser()
parser.add_argument("-f", "--file", dest="filename",
default="1ddata.txt",
help="use FILE as input data", metavar="FILE")
parser.add_argument("-b", "--bins", dest="bins", type=int,
default=15,
help="how many bins should be used")
parser.add_argument("-m", "--max", dest="max", type=float,
default=15000,
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)