Skip to content

Commit 3655e28

Browse files
authored
Merge pull request #286 from nschloe/datetime
support for datetime
2 parents 6a5ec42 + 84be26c commit 3655e28

File tree

7 files changed

+220
-169
lines changed

7 files changed

+220
-169
lines changed

README.md

Lines changed: 30 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,13 @@ This is matplotlib2tikz, a Python tool for converting matplotlib figures into
1818
for native inclusion into LaTeX documents.
1919

2020
The output of matplotlib2tikz is in
21-
[PGFPlots](http://pgfplots.sourceforge.net/pgfplots.pdf), a LaTeX library that
22-
sits on top of TikZ and describes graphs in terms of axes, data etc.
23-
Consequently, the output of matplotlib2tikz retains more information, can be
24-
more easily understood, and is more easily editable than [raw TikZ output](https://matplotlib.org/users/whats_new.html#pgf-tikz-backend).
21+
[PGFPlots](http://pgfplots.sourceforge.net/pgfplots.pdf), a LaTeX library that sits on
22+
top of [PGF/TikZ](https://en.wikipedia.org/wiki/PGF/TikZ) and describes graphs in terms
23+
of axes, data etc. Consequently, the output of matplotlib2tikz
24+
* retains more information,
25+
* can be more easily understood, and
26+
* is more easily editable
27+
than [raw TikZ output](https://matplotlib.org/users/whats_new.html#pgf-tikz-backend).
2528
For example, the matplotlib figure
2629
```python,test
2730
import matplotlib.pyplot as plt
@@ -45,26 +48,25 @@ tikz_save("test.tex")
4548
```
4649
(see above) gives
4750
```latex
48-
% This file was created by matplotlib2tikz vx.y.z.
4951
\begin{tikzpicture}
5052
51-
\definecolor{color1}{rgb}{0.203921568627451,0.541176470588235,0.741176470588235}
5253
\definecolor{color0}{rgb}{0.886274509803922,0.290196078431373,0.2}
54+
\definecolor{color1}{rgb}{0.203921568627451,0.541176470588235,0.741176470588235}
5355
5456
\begin{axis}[
55-
title={Simple plot $\frac{\alpha}{2}$},
56-
xlabel={time (s)},
57-
ylabel={Voltage (mV)},
58-
xmin=-0.095, xmax=1.995,
59-
ymin=-1.1, ymax=1.1,
57+
axis background/.style={fill=white!89.80392156862746!black},
58+
axis line style={white},
6059
tick align=outside,
6160
tick pos=left,
62-
xmajorgrids,
61+
title={Simple plot $\frac{\alpha}{2}$},
6362
x grid style={white},
64-
ymajorgrids,
63+
xlabel={time (s)},
64+
xmajorgrids,
65+
xmin=-0.095, xmax=1.995,
6566
y grid style={white},
66-
axis line style={white},
67-
axis background/.style={fill=white!89.803921568627459!black}
67+
ylabel={Voltage (mV)},
68+
ymajorgrids,
69+
ymin=-1.1, ymax=1.1
6870
]
6971
\addplot [line width=1.64pt, color0, mark=*, mark size=3, mark options={solid}]
7072
table {%
@@ -92,7 +94,7 @@ work flow.
9294
contains great examples of how to make your plot look even better.
9395

9496
Of course, not all figures produced by matplotlib can be converted without error.
95-
Notably, [3D plot don't work](https://github.com/matplotlib/matplotlib/issues/7243).
97+
Notably, [3D plots don't work](https://github.com/matplotlib/matplotlib/issues/7243).
9698

9799
### Installation
98100

@@ -111,54 +113,25 @@ to install/update.
111113

112114
2. Instead of `pyplot.show()`, invoke matplotlib2tikz by
113115
```python
114-
tikz_save('mytikz.tex')
115-
```
116-
to store the TikZ file as `mytikz.tex`. Load the library with:
117-
```python
118-
from matplotlib2tikz import save as tikz_save
119-
```
120-
_Optional:_
121-
The scripts accepts several options, for example `height`, `width`,
122-
`encoding`, and some others. Invoke by
123-
```python
124-
tikz_save('mytikz.tex', figureheight='4cm', figurewidth='6cm')
125-
```
126-
Note that height and width must be set large enough; setting it too low may
127-
result in a LaTeX compilation failure along the lines of `Dimension Too Large` or `Arithmetic Overflow`;
128-
see information about these errors in [the PGFPlots manual](http://pgfplots.sourceforge.net/pgfplots.pdf).
129-
To specify the dimension of the plot from within the LaTeX document, try
130-
```python
131-
tikz_save(
132-
'mytikz.tex',
133-
figureheight='\\figureheight',
134-
figurewidth='\\figurewidth'
135-
)
116+
import matplotlib2tikz
117+
matplotlib2tikz.save("mytikz.tex")
136118
```
137-
and in the LaTeX source
119+
to store the TikZ file as `mytikz.tex`.
120+
121+
3. Add the contents of `mytikz.tex` into your LaTeX source code. A convenient way of
122+
doing so is via
138123
```latex
139-
\newlength\figureheight
140-
\newlength\figurewidth
141-
\setlength\figureheight{4cm}
142-
\setlength\figurewidth{6cm}
143-
\input{mytikz.tex}
124+
\input{/path/to/mytikz.tex}
144125
```
145-
146-
3. Add the contents of `mytikz.tex` into your LaTeX source code; a convenient
147-
way of doing so is via `\input{/path/to/mytikz.tex}`. Also make sure that
148-
in the header of your document the packages for PGFPlots and proper Unicode
149-
support and are included:
126+
Also make sure that the packages for PGFPlots and proper Unicode support and are
127+
included in the header of your document:
150128
```latex
151129
\usepackage[utf8]{inputenc}
130+
\usepackage{fontspec} % optional
152131
\usepackage{pgfplots}
153-
```
154-
Additionally, with LuaLaTeX
155-
```latex
156-
\usepackage{fontspec}
157-
```
158-
is needed to typeset Unicode characters.
159-
Optionally, to use the latest PGFPlots features, insert
160-
```latex
161132
\pgfplotsset{compat=newest}
133+
\usepgfplotslibrary{groupplots}
134+
\usepgfplotslibrary{dateplot}
162135
```
163136

164137
### Contributing

matplotlib2tikz/axes.py

Lines changed: 71 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -230,13 +230,13 @@ def _ticks(self, data, obj):
230230
self.axis_options.append("tick align=center")
231231

232232
# Set each rotation for every label
233-
x_tick_rotation_and_horizontal_alignment = _get_label_rotation_and_horizontal_alignment(
233+
x_tick_rotation_and_horizontal_alignment = self._get_label_rotation_and_horizontal_alignment(
234234
obj, data, "x"
235235
)
236236
if x_tick_rotation_and_horizontal_alignment:
237237
self.axis_options.append(x_tick_rotation_and_horizontal_alignment)
238238

239-
y_tick_rotation_and_horizontal_alignment = _get_label_rotation_and_horizontal_alignment(
239+
y_tick_rotation_and_horizontal_alignment = self._get_label_rotation_and_horizontal_alignment(
240240
obj, data, "y"
241241
)
242242
if y_tick_rotation_and_horizontal_alignment:
@@ -397,86 +397,92 @@ def _subplot(self, obj, data):
397397

398398
return
399399

400+
def _get_label_rotation_and_horizontal_alignment(self, obj, data, x_or_y):
401+
tick_label_text_width = None
402+
tick_label_text_width_identifier = "{} tick label text width".format(x_or_y)
403+
if tick_label_text_width_identifier in self.axis_options:
404+
self.axis_options.remove(tick_label_text_width_identifier)
400405

401-
def _get_label_rotation_and_horizontal_alignment(obj, data, x_or_y):
402-
tick_label_text_width = None
403-
tick_label_text_width_identifier = "{} tick label text width".format(x_or_y)
404-
if tick_label_text_width_identifier in data["extra axis options"]:
405-
data["extra axis options"].remove(tick_label_text_width_identifier)
406+
label_style = ""
406407

407-
label_style = ""
408-
409-
major_tick_labels = (
410-
obj.xaxis.get_majorticklabels()
411-
if x_or_y == "x"
412-
else obj.yaxis.get_majorticklabels()
413-
)
414-
415-
if not major_tick_labels:
416-
return None
408+
major_tick_labels = (
409+
obj.xaxis.get_majorticklabels()
410+
if x_or_y == "x"
411+
else obj.yaxis.get_majorticklabels()
412+
)
417413

418-
tick_labels_rotation = [label.get_rotation() for label in major_tick_labels]
419-
tick_labels_rotation_same_value = len(set(tick_labels_rotation)) == 1
414+
if not major_tick_labels:
415+
return None
420416

421-
tick_labels_horizontal_alignment = [
422-
label.get_horizontalalignment() for label in major_tick_labels
423-
]
424-
tick_labels_horizontal_alignment_same_value = (
425-
len(set(tick_labels_horizontal_alignment)) == 1
426-
)
417+
tick_labels_rotation = [label.get_rotation() for label in major_tick_labels]
418+
tick_labels_rotation_same_value = len(set(tick_labels_rotation)) == 1
427419

428-
if tick_labels_rotation_same_value and tick_labels_horizontal_alignment_same_value:
429-
values = []
420+
tick_labels_horizontal_alignment = [
421+
label.get_horizontalalignment() for label in major_tick_labels
422+
]
423+
tick_labels_horizontal_alignment_same_value = (
424+
len(set(tick_labels_horizontal_alignment)) == 1
425+
)
430426

431-
if any(tick_labels_rotation) != 0:
432-
values.append("rotate={}".format(tick_labels_rotation[0]))
427+
if (
428+
tick_labels_rotation_same_value
429+
and tick_labels_horizontal_alignment_same_value
430+
):
431+
values = []
433432

434-
# Horizontal alignment will be ignored if no 'x/y tick label text width' has
435-
# been passed in the 'extra' parameter
436-
if tick_label_text_width:
437-
values.append("align={}".format(tick_labels_horizontal_alignment[0]))
438-
values.append("text width={}".format(tick_label_text_width))
433+
if any(tick_labels_rotation) != 0:
434+
values.append("rotate={}".format(tick_labels_rotation[0]))
439435

440-
if values:
441-
label_style = "{}ticklabel style = {{{}}}".format(x_or_y, ",".join(values))
442-
else:
443-
values = []
436+
# Horizontal alignment will be ignored if no 'x/y tick label text width' has
437+
# been passed in the 'extra' parameter
438+
if tick_label_text_width:
439+
values.append("align={}".format(tick_labels_horizontal_alignment[0]))
440+
values.append("text width={}".format(tick_label_text_width))
444441

445-
if tick_labels_rotation_same_value:
446-
values.append("rotate={}".format(tick_labels_rotation[0]))
447-
else:
448-
values.append(
449-
"rotate={{{},0}}[\\ticknum]".format(
450-
",".join(str(x) for x in tick_labels_rotation)
442+
if values:
443+
label_style = "{}ticklabel style = {{{}}}".format(
444+
x_or_y, ",".join(values)
451445
)
452-
)
446+
else:
447+
values = []
453448

454-
# Ignore horizontal alignment if no '{x,y} tick label text width' has
455-
# been passed in the 'extra' parameter
456-
if tick_label_text_width:
457-
if tick_labels_horizontal_alignment_same_value:
458-
values.append("align={}".format(tick_labels_horizontal_alignment[0]))
459-
values.append("text width={}".format(tick_label_text_width))
449+
if tick_labels_rotation_same_value:
450+
values.append("rotate={}".format(tick_labels_rotation[0]))
460451
else:
461-
for idx, x in enumerate(tick_labels_horizontal_alignment):
462-
label_style += "{}_tick_label_ha_{}/.initial = {}".format(
463-
x_or_y, idx, x
464-
)
465-
466452
values.append(
467-
"align=\\pgfkeysvalueof{{/pgfplots/{}_tick_label_ha_\\ticknum}}".format(
468-
x_or_y
453+
"rotate={{{},0}}[\\ticknum]".format(
454+
",".join(str(x) for x in tick_labels_rotation)
469455
)
470456
)
471-
values.append("text width={}".format(tick_label_text_width))
472457

473-
label_style = (
474-
"every {} tick label/.style = {{\n"
475-
"{}\n"
476-
"}}".format(x_or_y, ",\n".join(values))
477-
)
458+
# Ignore horizontal alignment if no '{x,y} tick label text width' has
459+
# been passed in the 'extra' parameter
460+
if tick_label_text_width:
461+
if tick_labels_horizontal_alignment_same_value:
462+
values.append(
463+
"align={}".format(tick_labels_horizontal_alignment[0])
464+
)
465+
values.append("text width={}".format(tick_label_text_width))
466+
else:
467+
for idx, x in enumerate(tick_labels_horizontal_alignment):
468+
label_style += "{}_tick_label_ha_{}/.initial = {}".format(
469+
x_or_y, idx, x
470+
)
471+
472+
values.append(
473+
"align=\\pgfkeysvalueof{{/pgfplots/{}_tick_label_ha_\\ticknum}}".format(
474+
x_or_y
475+
)
476+
)
477+
values.append("text width={}".format(tick_label_text_width))
478+
479+
label_style = (
480+
"every {} tick label/.style = {{\n"
481+
"{}\n"
482+
"}}".format(x_or_y, ",\n".join(values))
483+
)
478484

479-
return label_style
485+
return label_style
480486

481487

482488
def _get_tick_position(obj, axes_obj):

matplotlib2tikz/legend.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,17 @@ def draw_legend(data, obj):
7676
break
7777

7878
if alignment:
79-
data["extra axis options"].add("legend cell align={{{}}}".format(alignment))
79+
data["current axes"].axis_options.append(
80+
"legend cell align={{{}}}".format(alignment)
81+
)
8082

8183
if obj._ncol != 1:
82-
data["extra axis options"].add("legend columns={}".format(obj._ncol))
84+
data["current axes"].axis_options.append("legend columns={}".format(obj._ncol))
8385

8486
# Write styles to data
8587
if legend_style:
8688
style = "legend style={{{}}}".format(", ".join(legend_style))
87-
data["extra axis options"].add(style)
89+
data["current axes"].axis_options.append(style)
8890

8991
return data
9092

0 commit comments

Comments
 (0)