Skip to content

Commit e886805

Browse files
authored
Merge pull request #194 from benneti/master
add option to turn the axis environment of
2 parents 86991b3 + 1e18ff4 commit e886805

File tree

1 file changed

+29
-10
lines changed

1 file changed

+29
-10
lines changed

matplotlib2tikz/save.py

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ def get_tikz_code(
2828
tex_relative_path_to_data=None,
2929
strict=False,
3030
wrap=True,
31+
axis_environment=True,
3132
extra_axis_parameters=None,
3233
extra_tikzpicture_parameters=None,
3334
dpi=None,
@@ -84,6 +85,12 @@ def get_tikz_code(
8485
Default is ``True``.
8586
:type wrap: bool
8687
88+
:param axis_environment: Whether ``'\\begin{axis}[...]'`` and
89+
``'\\end{axis}'`` will be written. One needs to
90+
set the environment in the document. If ``False``
91+
additionally sets ``wrap=False``.
92+
:type axis_environment: bool
93+
8794
:param extra_axis_parameters: Extra axis options to be passed (as a set)
8895
to pgfplots. Default is ``None``.
8996
:type extra_axis_parameters: a set of strings for the pfgplots axes.
@@ -126,6 +133,8 @@ def get_tikz_code(
126133
data['custom colors'] = {}
127134
data['legend colors'] = []
128135
data['extra tikzpicture parameters'] = extra_tikzpicture_parameters
136+
data['axis environment'] = axis_environment
137+
data['show_info'] = show_info
129138
# rectangle_legends is used to keep track of which rectangles have already
130139
# had \addlegendimage added. There should be only one \addlegenimage per
131140
# bar chart data series.
@@ -143,6 +152,10 @@ def get_tikz_code(
143152
savefig_dpi if isinstance(savefig_dpi, int) \
144153
else mpl.rcParams['figure.dpi']
145154

155+
# print message about necessary pgfplot libs to command line
156+
if show_info:
157+
_print_pgfplot_libs_message(data)
158+
146159
# gather the file content
147160
data, content = _recurse(data, figure)
148161

@@ -158,7 +171,7 @@ def get_tikz_code(
158171
code += _tex_comment(disclaimer)
159172

160173
# write the contents
161-
if wrap:
174+
if wrap and axis_environment:
162175
code += '\\begin{tikzpicture}\n\n'
163176
if extra_tikzpicture_parameters:
164177
code += ',\n'.join(data['extra tikzpicture parameters'])
@@ -171,12 +184,9 @@ def get_tikz_code(
171184

172185
code += ''.join(content)
173186

174-
if wrap:
187+
if wrap and axis_environment:
175188
code += '\\end{tikzpicture}'
176189

177-
# print message about necessary pgfplot libs to command line
178-
if show_info:
179-
_print_pgfplot_libs_message(data)
180190
return code
181191

182192

@@ -275,11 +285,20 @@ def _recurse(data, obj):
275285
# add extra axis options from children
276286
if data['extra axis options']:
277287
ax.axis_options.extend(data['extra axis options'])
278-
# populate content
279-
content.extend(
280-
ax.get_begin_code() +
281-
children_content +
282-
[ax.get_end_code(data)], 0)
288+
# populate content and add axis environment if wished
289+
if data['axis environment']:
290+
content.extend(
291+
ax.get_begin_code() +
292+
children_content +
293+
[ax.get_end_code(data)], 0)
294+
else:
295+
content.extend(children_content, 0)
296+
# print axis environment options, if told to show infos
297+
if data['show_info']:
298+
print("=========================================================")
299+
print("These would have been the properties of the environment:")
300+
print(''.join(ax.get_begin_code()[1:]))
301+
print("=========================================================")
283302
elif isinstance(child, mpl.lines.Line2D):
284303
data, cont = line2d.draw_line2d(data, child)
285304
content.extend(cont, child.get_zorder())

0 commit comments

Comments
 (0)