-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCSVTikzSpline.py
More file actions
133 lines (112 loc) · 4.44 KB
/
CSVTikzSpline.py
File metadata and controls
133 lines (112 loc) · 4.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
"""
Transform a CSV file with the following format
category;col1;col2;col3;col4
cat1;1;1;1;2
cat2;2;2;2;2
...
catn;n;n;n;n
into a tikz graph
Author: Symeon Malengreau
Version: 0.1.2
"""
import sys
import random
CONSTANT_SIZE_X = 15.0
CONSTANT_SIZE_Y = 10.0
COLORS = ['red', 'green', 'blue', 'cyan','magenta', 'yellow', 'darkgray', 'brown', 'lime', 'olive', 'orange', 'pink', 'purple', 'teal', 'violet']
def readCSV(path) :
f = open(path, 'r')
h = f.readline().split(";")
out = []
for i in range(len(h)) :
out.append([])
for line in f :
tmp = line.split(';')
for i in range(len(tmp)):
if tmp[i] != '' :
out[i].append(float(tmp[i]))
else :
out[i].append(0)
f.close()
colors = []
for i in range(len(out)-1):
if len(COLORS) > 0 :
r = random.randint(1,len(COLORS))
color = COLORS[r-1]
COLORS.remove(color)
colors.append(color)
else :
colors.append('black')
return {'head':h, 'data':out, 'colors':colors}
def writeCategory(f, category) :
# Print category
f.write('% Category\n')
numCategory = len(category)
f.write('\\node at (0,-0.5) {0};\n')
f.write('\\draw(0,0) -- (0,-0.1);\n')
for i in range(numCategory) :
pos = str((i+1) * CONSTANT_SIZE_X/numCategory)
f.write('\\draw(' + pos +',0) -- ('+ pos + ',-0.1);\n')
f.write('\\node at (' + pos + ',-0.5) {' + str(category[i]) + '};\n')
def writeAxis(f, values, minVal, maxVal, color) :
numCategory = len(values)
spacing = (abs(minVal) + maxVal)/CONSTANT_SIZE_Y
f.write('% Axis\n')
f.write('\\draw[' + color + ',line width=0.25mm] (0,0) -- (' + str(1 * CONSTANT_SIZE_X/numCategory) + ',' + str(values[0]/spacing) + ');\n');
for i in range(numCategory-1) :
f.write('\\draw[' + color + ',line width=0.25mm] (' + str((i+1) * CONSTANT_SIZE_X/numCategory) + ',' + str(values[i]/spacing) + ') -- (' + str((i+2) * CONSTANT_SIZE_X/numCategory) + ',' + str(values[i+1]/spacing) + ');\n');
def writeAxisSpline(f, values, minVal, maxVal, color) :
numCategory = len(values)
spacing = (abs(minVal) + maxVal)/CONSTANT_SIZE_Y
f.write('% Axis\n')
f.write('\\draw[' + color + ',line width=0.25mm] plot [smooth] coordinates {(0,0)')
for i in range(numCategory) :
f.write('(' + str((i+1) * CONSTANT_SIZE_X/numCategory) + ',' + str(values[i]/spacing) + ')')
f.write('};\n')
def writeValue(f, minVal, maxVal) :
spacing = int((abs(minVal) + maxVal)/CONSTANT_SIZE_Y)
f.write('% Value\n')
for i in range(int(CONSTANT_SIZE_Y)+1) :
f.write('\\draw(-0.1,' + str(i) + ') -- (0,' + str(i) + ');\n')
f.write('\\node at (-0.6, ' +str(i) +') {' + str(minVal+spacing*i) + '};\n')
def writeLegend(f, colors, head) :
for i in range(len(colors)) :
f.write('\\draw[' + colors[i] + ', line width=0.25mm](17,' + str(i*0.4+2) + ') -- (18,' + str(i*0.4+2) + ');\n')
f.write('\\node at (19,' + str(i*0.4+2) + ') {' + head[i+1] + '};\n')
def writeData(f, data) :
# Write category
writeCategory(f, data['data'][0])
minVal = sys.maxint
maxVal = -sys.maxint - 1
for i in range(len(data['data'])-1) :
for val in data['data'][i+1] :
if val < minVal :
minVal = val
if val > maxVal :
maxVal = val
# Write Value
if minVal >= 0 :
minVal = 0
writeValue(f, minVal, maxVal)
# Write Axis
for i in range(len(data['data'])-1) :
writeAxisSpline(f, data['data'][i+1], minVal, maxVal, data['colors'][i])
writeLegend(f, data['colors'], data['head'])
def createTikz(path, data) :
f = open(path, 'w')
f.write('\\begin{tikzpicture}[scale=1.0]\n')
f.write('% NAME TO REPLACE\n')
f.write('\\node at (-0.2,11.2) {\\textit{Axis y}};\n')
f.write('\\node at (16.8,0) {\\textit{Axis x}};\n')
f.write('%\\Large\n%\\node at (10,12) {\\textbf{Main Title}};\n%\\normalsize\n')
f.write('% GRAPH\n')
f.write('\\draw[step=0.25,black!8,very thin] (0,0) grid (15.5,10.5);\n')
f.write('\\draw[->](0,0) -- (15.8, 0);\n')
f.write('\\draw[->](0,0) -- (0, 10.8);\n')
writeData(f, data)
f.write('\\end{tikzpicture}')
f.close()
inFile = raw_input('Enter input .csv file: ')
outFile = raw_input('Enter output .tex file: ')
out = readCSV(inFile)
createTikz(outFile, out)