mirror of
https://github.com/MartinThoma/LaTeX-examples.git
synced 2025-04-19 11:38:05 +02:00
tikz/histogram-symbols: Added
This commit is contained in:
parent
4c614327b1
commit
589a7179c5
6 changed files with 649 additions and 0 deletions
369
tikz/histogram-symbols/1ddata.txt
Normal file
369
tikz/histogram-symbols/1ddata.txt
Normal file
|
@ -0,0 +1,369 @@
|
|||
3514
|
||||
3400
|
||||
2914
|
||||
2601
|
||||
2567
|
||||
2518
|
||||
2413
|
||||
2378
|
||||
2296
|
||||
2179
|
||||
2139
|
||||
2041
|
||||
2008
|
||||
1910
|
||||
1759
|
||||
1576
|
||||
1533
|
||||
1528
|
||||
1527
|
||||
1522
|
||||
1509
|
||||
1452
|
||||
1402
|
||||
1399
|
||||
1385
|
||||
1337
|
||||
1306
|
||||
1286
|
||||
1246
|
||||
1235
|
||||
1217
|
||||
1186
|
||||
1147
|
||||
1140
|
||||
1136
|
||||
1131
|
||||
1123
|
||||
1112
|
||||
1101
|
||||
1098
|
||||
1076
|
||||
1068
|
||||
1067
|
||||
1066
|
||||
1054
|
||||
1016
|
||||
1008
|
||||
996
|
||||
996
|
||||
980
|
||||
978
|
||||
950
|
||||
948
|
||||
929
|
||||
923
|
||||
916
|
||||
915
|
||||
910
|
||||
903
|
||||
887
|
||||
880
|
||||
875
|
||||
872
|
||||
867
|
||||
866
|
||||
861
|
||||
843
|
||||
823
|
||||
804
|
||||
799
|
||||
773
|
||||
773
|
||||
772
|
||||
763
|
||||
762
|
||||
762
|
||||
758
|
||||
755
|
||||
743
|
||||
741
|
||||
733
|
||||
692
|
||||
687
|
||||
686
|
||||
671
|
||||
667
|
||||
651
|
||||
641
|
||||
631
|
||||
624
|
||||
621
|
||||
607
|
||||
569
|
||||
562
|
||||
557
|
||||
549
|
||||
544
|
||||
532
|
||||
523
|
||||
519
|
||||
514
|
||||
511
|
||||
505
|
||||
489
|
||||
486
|
||||
474
|
||||
461
|
||||
461
|
||||
453
|
||||
448
|
||||
441
|
||||
424
|
||||
420
|
||||
417
|
||||
415
|
||||
411
|
||||
408
|
||||
404
|
||||
402
|
||||
399
|
||||
398
|
||||
398
|
||||
393
|
||||
391
|
||||
387
|
||||
383
|
||||
380
|
||||
370
|
||||
362
|
||||
362
|
||||
359
|
||||
359
|
||||
356
|
||||
350
|
||||
348
|
||||
347
|
||||
345
|
||||
336
|
||||
336
|
||||
335
|
||||
330
|
||||
330
|
||||
329
|
||||
321
|
||||
320
|
||||
316
|
||||
314
|
||||
312
|
||||
311
|
||||
304
|
||||
298
|
||||
296
|
||||
288
|
||||
286
|
||||
282
|
||||
282
|
||||
279
|
||||
277
|
||||
275
|
||||
274
|
||||
270
|
||||
268
|
||||
267
|
||||
264
|
||||
261
|
||||
260
|
||||
259
|
||||
252
|
||||
251
|
||||
250
|
||||
243
|
||||
242
|
||||
241
|
||||
240
|
||||
240
|
||||
240
|
||||
240
|
||||
239
|
||||
238
|
||||
237
|
||||
237
|
||||
231
|
||||
227
|
||||
227
|
||||
223
|
||||
222
|
||||
222
|
||||
221
|
||||
221
|
||||
219
|
||||
217
|
||||
215
|
||||
214
|
||||
213
|
||||
209
|
||||
209
|
||||
209
|
||||
204
|
||||
204
|
||||
203
|
||||
202
|
||||
201
|
||||
201
|
||||
199
|
||||
198
|
||||
195
|
||||
194
|
||||
193
|
||||
193
|
||||
185
|
||||
185
|
||||
184
|
||||
184
|
||||
184
|
||||
184
|
||||
183
|
||||
183
|
||||
181
|
||||
180
|
||||
176
|
||||
175
|
||||
175
|
||||
171
|
||||
167
|
||||
167
|
||||
167
|
||||
164
|
||||
163
|
||||
163
|
||||
160
|
||||
160
|
||||
159
|
||||
159
|
||||
156
|
||||
156
|
||||
155
|
||||
154
|
||||
152
|
||||
150
|
||||
150
|
||||
150
|
||||
149
|
||||
148
|
||||
147
|
||||
147
|
||||
146
|
||||
144
|
||||
143
|
||||
142
|
||||
142
|
||||
141
|
||||
141
|
||||
140
|
||||
139
|
||||
139
|
||||
139
|
||||
139
|
||||
138
|
||||
135
|
||||
134
|
||||
133
|
||||
133
|
||||
132
|
||||
132
|
||||
127
|
||||
127
|
||||
126
|
||||
124
|
||||
124
|
||||
123
|
||||
121
|
||||
121
|
||||
121
|
||||
121
|
||||
120
|
||||
120
|
||||
119
|
||||
118
|
||||
118
|
||||
118
|
||||
118
|
||||
115
|
||||
115
|
||||
115
|
||||
114
|
||||
114
|
||||
114
|
||||
113
|
||||
112
|
||||
111
|
||||
111
|
||||
111
|
||||
110
|
||||
108
|
||||
107
|
||||
105
|
||||
105
|
||||
105
|
||||
104
|
||||
103
|
||||
101
|
||||
101
|
||||
101
|
||||
101
|
||||
100
|
||||
100
|
||||
99
|
||||
99
|
||||
98
|
||||
97
|
||||
97
|
||||
96
|
||||
95
|
||||
94
|
||||
90
|
||||
90
|
||||
90
|
||||
86
|
||||
84
|
||||
83
|
||||
78
|
||||
75
|
||||
72
|
||||
72
|
||||
69
|
||||
67
|
||||
67
|
||||
66
|
||||
66
|
||||
66
|
||||
65
|
||||
64
|
||||
63
|
||||
63
|
||||
63
|
||||
62
|
||||
62
|
||||
61
|
||||
61
|
||||
61
|
||||
61
|
||||
60
|
||||
60
|
||||
59
|
||||
59
|
||||
59
|
||||
58
|
||||
58
|
||||
58
|
||||
58
|
||||
58
|
||||
57
|
||||
57
|
||||
57
|
||||
56
|
||||
56
|
||||
56
|
||||
56
|
||||
56
|
||||
56
|
||||
56
|
||||
56
|
||||
56
|
||||
55
|
||||
54
|
||||
54
|
||||
53
|
||||
53
|
||||
51
|
35
tikz/histogram-symbols/Makefile
Normal file
35
tikz/histogram-symbols/Makefile
Normal 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
|
12
tikz/histogram-symbols/README.md
Normal file
12
tikz/histogram-symbols/README.md
Normal file
|
@ -0,0 +1,12 @@
|
|||
Compiled example
|
||||
----------------
|
||||

|
||||
|
||||
Credits
|
||||
--------
|
||||
|
||||
Thanks to [esdd](http://tex.stackexchange.com/users/43317/esdd) who helped me
|
||||
to get the LaTeX part work (see [tex.SE answer](http://tex.stackexchange.com/a/194966/5645))
|
||||
|
||||
Thanks to [jenson_bo](http://tex.stackexchange.com/users/17775/jenson-bo) for
|
||||
making it beautiful!
|
|
@ -0,0 +1,55 @@
|
|||
\documentclass[margin=10pt]{standalone}
|
||||
\usepackage{amsmath}
|
||||
\usepackage{pgfplots}
|
||||
\pgfplotsset{compat=1.3}
|
||||
|
||||
\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);
|
||||
}
|
||||
|
||||
\definecolor{mycolor}{rgb}{0.02,0.4,0.7}
|
||||
|
||||
\begin{document}
|
||||
\begin{tikzpicture}
|
||||
\begin{axis}[
|
||||
ymajorgrids,
|
||||
xmajorgrids,
|
||||
grid style={white,thick},
|
||||
axis on top,
|
||||
/tikz/ybar interval,
|
||||
tick align=outside,
|
||||
ymin=0,
|
||||
axis line style={draw opacity=0},
|
||||
tick style={draw=none},
|
||||
enlarge x limits=false,
|
||||
height=5cm,
|
||||
title style={font=\Large},
|
||||
xlabel={Collected Labeled Recordings},
|
||||
ylabel={Number of Symbols},
|
||||
ytick={ {{yticks}} },
|
||||
scaled ticks=false,
|
||||
yticklabels={ {{yticklabels}} },
|
||||
xticklabels={ {{xticklabels}} },
|
||||
width=\textwidth,
|
||||
xtick=data,
|
||||
label style={font=\large},
|
||||
ticklabel style={
|
||||
inner sep=1pt,
|
||||
font=\small
|
||||
},
|
||||
nodes near coords,
|
||||
every node near coord/.append style={
|
||||
fill=white,
|
||||
anchor=mid west,
|
||||
shift={(3pt,4pt)},
|
||||
inner sep=0,
|
||||
font=\footnotesize,
|
||||
rotate=45},
|
||||
]
|
||||
\addplot[mycolor!80!white, fill=mycolor, draw=none] coordinates { {{coordinates}} };
|
||||
\end{axis}
|
||||
\clipright
|
||||
\end{tikzpicture}
|
||||
\end{document}
|
55
tikz/histogram-symbols/histogram-large-1d-dataset.tex
Normal file
55
tikz/histogram-symbols/histogram-large-1d-dataset.tex
Normal file
|
@ -0,0 +1,55 @@
|
|||
\documentclass[margin=10pt]{standalone}
|
||||
\usepackage{amsmath}
|
||||
\usepackage{pgfplots}
|
||||
\pgfplotsset{compat=1.3}
|
||||
|
||||
\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);
|
||||
}
|
||||
|
||||
\definecolor{mycolor}{rgb}{0.02,0.4,0.7}
|
||||
|
||||
\begin{document}
|
||||
\begin{tikzpicture}
|
||||
\begin{axis}[
|
||||
ymajorgrids,
|
||||
xmajorgrids,
|
||||
grid style={white,thick},
|
||||
axis on top,
|
||||
/tikz/ybar interval,
|
||||
tick align=outside,
|
||||
ymin=0,
|
||||
axis line style={draw opacity=0},
|
||||
tick style={draw=none},
|
||||
enlarge x limits=false,
|
||||
height=5cm,
|
||||
title style={font=\Large},
|
||||
xlabel={Collected Labeled Recordings},
|
||||
ylabel={Number of Symbols},
|
||||
ytick={ 0,20,40,60,80,100 },
|
||||
scaled ticks=false,
|
||||
yticklabels={ 0, 20, 40, 60, 80, 100 },
|
||||
xticklabels={ $0$, $100$, $200$, $300$, $400$, $500$, $600$, $\infty$ },
|
||||
width=\textwidth,
|
||||
xtick=data,
|
||||
label style={font=\large},
|
||||
ticklabel style={
|
||||
inner sep=1pt,
|
||||
font=\small
|
||||
},
|
||||
nodes near coords,
|
||||
every node near coord/.append style={
|
||||
fill=white,
|
||||
anchor=mid west,
|
||||
shift={(3pt,4pt)},
|
||||
inner sep=0,
|
||||
font=\footnotesize,
|
||||
rotate=45},
|
||||
]
|
||||
\addplot[mycolor!80!white, fill=mycolor, draw=none] coordinates { (0, 122) (1, 66) (2, 39) (3, 30) (4, 12) (5, 9) (6, 10) (7, 70) };
|
||||
\end{axis}
|
||||
\clipright
|
||||
\end{tikzpicture}
|
||||
\end{document}
|
123
tikz/histogram-symbols/process.py
Executable file
123
tikz/histogram-symbols/process.py
Executable file
|
@ -0,0 +1,123 @@
|
|||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from __future__ import print_function
|
||||
import shutil
|
||||
import fileinput
|
||||
import math
|
||||
|
||||
|
||||
def main(filename, bins, maximum, yticks_number):
|
||||
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)]
|
||||
xticklabels = []
|
||||
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
|
||||
minimum = 0
|
||||
for b in range(bins):
|
||||
lower = minimum + (maximum - minimum)/bins*b
|
||||
xticklabels.append(get_xticklabel(lower))
|
||||
|
||||
# Get labels for y-axis
|
||||
yticks = []
|
||||
ytickslabels = []
|
||||
maxy = max(bin_counter)
|
||||
maxylabel = int(10**math.floor(math.log(maxy, 10)))*int(str(maxy)[0])
|
||||
ylabelsteps = int(math.ceil(maxylabel / yticks_number))
|
||||
if ylabelsteps == 0:
|
||||
ylabelsteps = 1
|
||||
for i in range(0, maxylabel+1, ylabelsteps):
|
||||
print("i: %i, %i" % (i, maxylabel))
|
||||
print("label: %i%s" % get_si_suffix(i))
|
||||
yticks.append(str(i))
|
||||
ytickslabels.append(get_yticklabel(i, True))
|
||||
|
||||
xticklabels.append("\infty")
|
||||
return bin_counter, xticklabels, ytickslabels, yticks
|
||||
|
||||
|
||||
def get_xticklabel(value):
|
||||
return str(int(value))
|
||||
|
||||
|
||||
def get_yticklabel(value, si_suffix):
|
||||
value = float(value)
|
||||
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, yticks):
|
||||
shutil.copyfile("histogram-large-1d-dataset.template.tex",
|
||||
"histogram-large-1d-dataset.tex")
|
||||
xticklabels = ", ".join(map(lambda n: "$%s$" % n, xticklabels))
|
||||
yticklabels = ", ".join(yticklabels)
|
||||
yticks = ",".join(yticks)
|
||||
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("{{yticks}}", yticks)
|
||||
line = line.replace("{{coordinates}}", coordinates)
|
||||
print(line, end='')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter
|
||||
parser = ArgumentParser(description=__doc__,
|
||||
formatter_class=ArgumentDefaultsHelpFormatter)
|
||||
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?"))
|
||||
parser.add_argument("--yticks", dest="yticks", type=int,
|
||||
default=5,
|
||||
help=("How many y-ticks should be used?"))
|
||||
args = parser.parse_args()
|
||||
bin_counter, xticklabels, yticklabels, yticks = main(args.filename,
|
||||
args.bins,
|
||||
args.max,
|
||||
args.yticks)
|
||||
modify_template(bin_counter, xticklabels, yticklabels, yticks)
|
Loading…
Add table
Add a link
Reference in a new issue