-
Notifications
You must be signed in to change notification settings - Fork 0
/
latex_script_python.py
286 lines (250 loc) · 7.8 KB
/
latex_script_python.py
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
import csv
import sqlite3
import sys
import json
from pprint import pprint
conn = sqlite3.connect("test.db")
conn.text_factory = str
cur = conn.cursor()
cur.execute("DROP TABLE IF EXISTS new;")
with open('sample.csv') as f:
reader = csv.reader(f)
row1 = next(reader) #gets the first line
print row1
print len(row1)
string = ','.join(row1)
if string[len(string) - 1] != ',' :
string = string + ','
string = string.replace(',', ' varchar(10),')
temp = list(string)
del temp[len(string) - 1]
string = ''.join(temp)
print string
cur.execute("CREATE TABLE new (%s);" % string)
cur.execute("select * from new;")
one = ''
for i in range(0, len(row1)) :
one =
with open('sample.csv', 'r') as csvfile :
dic = csv.DictReader(csvfile)
print dic
to_db = [(i['xcord'],i['ycord'],i['a'],i['b']) for i in dic]
print to_db
no_question_marks = '?,'*len(row1)
no_question_marks = no_question_marks[0:len(no_question_marks) - 1]
print no_question_marks
cur.executemany("INSERT into new values(%s);" % no_question_marks, to_db)
db_new = [str(map(int, i)) for i in to_db]
print to_db
conn.commit()
script = open("a.txt", "w")
sequence = ["\\documentclass{article}\n","\\usepackage[margin=0.5in]{geometry}\n","\\usepackage[utf8]{inputenc}\n","\\usepackage{pgfplots}\n","\\pgfplotsset{width=10cm,compat=1.9}\n","\\begin{document}\n"]
script.writelines(sequence)
#******************************************************************************#
def line_plot() :
xmin = 0 #liable to change
ymin = 0
xmax = to_db[len(to_db) - 1][0]
ymax = to_db[len(to_db) - 1][1]
xtick = (int(xmin) + int(xmax))/len(to_db)
ytick = (int(ymin) + int(ymax))/len(to_db)
l1 = [int(xmin),]
for i in range(1, len(to_db)) :
s = int(l1[i - 1]) + xtick
l1.append(s)
temp = str(l1)
t = list(temp)
t[0] = ''
t[len(t) - 1] = ''
temp = ''.join(t)
xtick = ''.join(("{",temp,"}"))
l2 = [int(ymin),]
for i in range(1, len(to_db)) :
s = int(l2[i - 1]) + ytick
l2.append(s)
temp = str(l2)
t = list(temp)
t[0] = ''
t[len(t) - 1] = ''
temp = ''.join(t)
ytick = ''.join(("{",temp,"}"))
string = ''.join(db_new)
new = list(string)
for i in range(0, len(new)) :
if new[i] == '[' :
new[i] = '('
if new[i] == ']' :
new[i] = ')'
if new[i] == ' ' :
new[i] = ''
coordinates = ''.join(new)
script.seek(0, 2)
coordinates1 = coordinates.replace("(", "")
coordinates1 = coordinates1.replace(")", " \n")
coordinates1 = coordinates1.replace(",", " ")
temp = list(coordinates1)
del temp[len(temp) - 1]
coordinates1 = ''.join(temp)
print coordinates1
s = ""
for i in coordinates1.split("\n") :
color_of_mark = raw_input("Enter the color of the mark : ")
i = i + color_of_mark
s = s + i + "\n"
temp = list(s)
del temp[len(temp) - 1]
s = ''.join(temp)
print s
nature_of_line = "dotted," #user input with comma
legend_position = "north west" #user input
grid_style = "dashed" #user input
color = "red" #user input
mark_style = "square" #user input
scale = 2 #user input
#with open('data.json') as input_file:
# file_data = json.load(input_file);
#*********json file parsing**********#
#pprint(file_data)
string = '''\\begin{tikzpicture}
\\begin{axis}[
title={Prototype},
xlabel={X-axis},
ylabel={Y-axis},
xmin=%d, xmax=%d,
ymin=%d, ymax=%d,
xtick=%s,
ytick=%s,
legend pos=%s,
ymajorgrids=true,
grid style=%s,
]
\\addplot[
%s color=%s, mark=%s, mark options={scale=%d}
]
coordinates {
%s
};
\legend{saurabh}
\end{axis}
\end{tikzpicture}''' % (int(xmin), int(xmax), int(ymin), int(ymax), xtick, ytick, legend_position, grid_style, nature_of_line, color, mark_style, scale, coordinates)
script.write(string)
#******************************************************************************#
def scatter_plot() :
script.seek(0, 2)
no_labels = input("No. of labels : ")
label = {}
for i in range(0, no_labels) :
i = raw_input("Name of label : ")
label[i] = []
mark = raw_input("Enter the mark : ")
color = raw_input("Enter the color : ")
label[i].append("mark=" + mark)
label[i].append(color)
output = str(label)
output = output.replace("[", "")
output = output.replace("]", "")
output = output.replace("{", " ")
output = output.replace("}", "")
output = output.replace(" ", "")
output = output.replace("'", "")
output = output.replace(":", "={")
temp = list(output)
count = 0
check = 0
for i in temp :
if i == ',' :
count = count + 1
if count % 2 == 0 and count != 0 and i == ',':
j = i.replace(",", "},")
temp[check] = j
check = check + 1
temp.append('}')
output = ''.join(temp)
print output
string = ''.join(db_new)
new = list(string)
for i in range(0, len(new)) :
if new[i] == '[' :
new[i] = '('
if new[i] == ']' :
new[i] = ')'
if new[i] == ' ' :
new[i] = ''
coordinates = ''.join(new)
coordinates1 = coordinates.replace("(", "")
coordinates1 = coordinates1.replace(")", " \n")
coordinates1 = coordinates1.replace(",", " ")
temp = list(coordinates1)
del temp[len(temp) - 1]
coordinates1 = ''.join(temp)
print coordinates1
s = ""
for i in coordinates1.split("\n") :
color_of_mark = raw_input("Enter the label : ")
i = i + color_of_mark
s = s + i + "\n"
temp = list(s)
del temp[len(temp) - 1]
s = ''.join(temp)
string = '''
\\begin{tikzpicture}
\\begin{axis}[
clickable coords={(xy): \\thisrow{label}},
scatter/classes={
%s}
]
\\addplot[scatter,only marks,scatter src=explicit symbolic]
table[meta=label] {
x y label
%s
};
\end{axis}
\end{tikzpicture}''' % (output, s)
script.write(string)
#******************************************************************************#
def bar_chart() :
script.seek(0, 2)
string = ''.join(db_new)
new = list(string)
for i in range(0, len(new)) :
if new[i] == '[' :
new[i] = '('
if new[i] == ']' :
new[i] = ')'
if new[i] == ' ' :
new[i] = ''
coordinates = ''.join(new)
y_label = "sqlite"
space_over_bars = 0.05
xcord_legend = 0.5
ycord_legend = -0.1
anchor_position = "north"
interval_between_bars = 0.7
legend_1 = "one"
legend_2 = "two"
string = '''
\\begin{tikzpicture}
\\begin{axis}[
x tick label style={/pgf/number format/1000 sep=},
ylabel=%s,
enlargelimits = %f,
legend style={at={(%d,%d)}, anchor=%s, legend columns=-1},
ybar interval=%f,
]
\\addplot
coordinates {%s};
\legend{%s,%s}
\end{axis}
\end{tikzpicture}''' % (y_label, space_over_bars, xcord_legend, ycord_legend, anchor_position, interval_between_bars, coordinates, legend_1, legend_2)
script.write(string)
#******************************************************************************#
#call functions here
#line_plot()
#script.seek(0, 2)
#script.write("\n\hskip 5pt\n")
#scatter_plot()
#script.seek(0, 2)
#script.write("\n\hskip 5pt\n")
bar_chart()
script.seek(0, 2)
script.write("\n\end{document}\n")