9
9
from . import patch
10
10
from . import text as mytext
11
11
12
+ from .__about__ import __version__
13
+
14
+ import codecs
12
15
import os
13
16
import matplotlib as mpl
14
- from .__about__ import __version__
15
17
16
18
17
- def save (filepath ,
18
- figure = 'gcf' ,
19
- encoding = None ,
20
- figurewidth = None ,
21
- figureheight = None ,
22
- textsize = 10.0 ,
23
- tex_relative_path_to_data = None ,
24
- strict = False ,
25
- wrap = True ,
26
- extra = None ,
27
- dpi = None ,
28
- show_info = True
29
- ):
19
+ def get_tikz_code (
20
+ filepath ,
21
+ figure = 'gcf' ,
22
+ encoding = None ,
23
+ figurewidth = None ,
24
+ figureheight = None ,
25
+ textsize = 10.0 ,
26
+ tex_relative_path_to_data = None ,
27
+ strict = False ,
28
+ wrap = True ,
29
+ extra = None ,
30
+ dpi = None ,
31
+ show_info = True
32
+ ):
30
33
'''Main function. Here, the recursion into the image starts and the
31
34
contents are picked up. The actual file gets written in this routine.
32
35
@@ -132,13 +135,6 @@ def save(filepath,
132
135
else :
133
136
data ['dpi' ] = dpi
134
137
135
- # open file
136
- import codecs
137
- file_handle = codecs .open (filepath , 'w' , encoding )
138
-
139
- if show_info :
140
- print ('file encoding: {0}' .format (file_handle .encoding ))
141
-
142
138
# gather the file content
143
139
data , content = _recurse (data , figure )
144
140
@@ -150,32 +146,45 @@ def save(filepath,
150
146
disclaimer = 'This file was created by matplotlib2tikz v%s.' % __version__
151
147
152
148
# write disclaimer to the file header
153
- file_handle .write (_tex_comment (disclaimer ))
149
+ code = ''''''
150
+ code += _tex_comment (disclaimer )
154
151
155
152
# write the contents
156
153
if wrap :
157
- file_handle . write ( '\\ begin{tikzpicture}\n \n ' )
154
+ code += '\\ begin{tikzpicture}\n \n '
158
155
159
156
coldefs = _get_color_definitions (data )
160
157
if coldefs :
161
- file_handle . write ( '\n ' .join (coldefs ) )
162
- file_handle . write ( '\n \n ' )
158
+ code += '\n ' .join (coldefs )
159
+ code += '\n \n '
163
160
164
- try :
165
- file_handle .write ('' .join (content ))
166
- except UnicodeEncodeError :
167
- # We're probably using Python 2, so use proper unicode treatment
168
- file_handle .write (unicode ('' .join (content )).encode ('utf-8' ))
161
+ code += '' .join (content )
169
162
170
163
if wrap :
171
- file_handle .write ('\\ end{tikzpicture}' )
172
-
173
- # close file
174
- file_handle .close ()
164
+ code += '\\ end{tikzpicture}'
175
165
176
166
# print message about necessary pgfplot libs to command line
177
167
if show_info :
178
168
_print_pgfplot_libs_message (data )
169
+ return code
170
+
171
+
172
+ def save (* args , ** kwargs ):
173
+ '''Same as `get_tikz_code()`, but actually saves the code to a file.
174
+ '''
175
+ code = get_tikz_code (* args , ** kwargs )
176
+
177
+ file_handle = codecs .open (
178
+ args [0 ],
179
+ 'w' ,
180
+ kwargs ['encoding' ] if 'encoding' in kwargs else None
181
+ )
182
+ try :
183
+ file_handle .write (code )
184
+ except UnicodeEncodeError :
185
+ # We're probably using Python 2, so use proper unicode treatment
186
+ file_handle .write (unicode (code ).encode ('utf-8' ))
187
+ file_handle .close ()
179
188
return
180
189
181
190
0 commit comments