Skip to content

Commit dbfb9ea

Browse files
committed
Add ConTeXt support
1 parent 22b7a5c commit dbfb9ea

29 files changed

+170
-76
lines changed

README.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ figures like
1818

1919
![](https://nschloe.github.io/tikzplotlib/example.png)
2020

21-
for native inclusion into LaTeX documents.
21+
for native inclusion into LaTeX or ConTeXt documents.
2222

2323
The output of tikzplotlib is in
24-
[PGFPlots](http://pgfplots.sourceforge.net/pgfplots.pdf), a LaTeX library that sits on
24+
[PGFPlots](http://pgfplots.sourceforge.net/pgfplots.pdf), a TeX library that sits on
2525
top of [PGF/TikZ](https://en.wikipedia.org/wiki/PGF/TikZ) and describes graphs in terms
2626
of axes, data etc. Consequently, the output of tikzplotlib
2727

@@ -95,7 +95,7 @@ table {%
9595
```
9696
(Use `get_tikz_code()` instead of `save()` if you want the code as a string.)
9797

98-
Tweaking the plot is straightforward and can be done as part of your LaTeX work flow.
98+
Tweaking the plot is straightforward and can be done as part of your TeX work flow.
9999
[The fantastic PGFPlots manual](http://pgfplots.sourceforge.net/pgfplots.pdf) contains
100100
great examples of how to make your plot look even better.
101101

@@ -120,23 +120,23 @@ to install/update.
120120
```python
121121
import tikzplotlib
122122
tikzplotlib.save("mytikz.tex")
123+
# or
124+
tikzplotlib.save("mytikz.tex", flavor="context")
123125
```
124126
to store the TikZ file as `mytikz.tex`.
125127

126-
3. Add the contents of `mytikz.tex` into your LaTeX source code. A convenient way of
128+
3. Add the contents of `mytikz.tex` into your TeX source code. A convenient way of
127129
doing so is via
128130
```latex
129131
\input{/path/to/mytikz.tex}
130132
```
131133
Also make sure that the packages for PGFPlots and proper Unicode support and are
132-
included in the header of your document:
133-
```latex
134-
\usepackage[utf8]{inputenc}
135-
\usepackage{fontspec} % optional
136-
\usepackage{pgfplots}
137-
\pgfplotsset{compat=newest}
138-
\usepgfplotslibrary{groupplots}
139-
\usepgfplotslibrary{dateplot}
134+
included in the header of your document. Get the code via:
135+
```python
136+
import tikzplotlib
137+
tikzplotlib.Flavors.latex.preamble()
138+
# or
139+
tikzplotlib.Flavors.context.preamble()
140140
```
141141

142142
4. Optional: clean up the figure before exporting to tikz using the `clean_figure` command.

test/helpers.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,14 @@ def _unidiff_output(expected, actual):
3131

3232

3333
def assert_equality(
34-
plot, filename, assert_compilation=True, **extra_get_tikz_code_args
34+
plot, filename, assert_compilation=True, flavor="latex", **extra_get_tikz_code_args
3535
):
3636
plot()
3737
code = tikzplotlib.get_tikz_code(
38-
include_disclaimer=False, float_format=".8g", **extra_get_tikz_code_args
38+
include_disclaimer=False,
39+
float_format=".8g",
40+
flavor=flavor,
41+
**extra_get_tikz_code_args
3942
)
4043
plt.close()
4144

@@ -47,13 +50,16 @@ def assert_equality(
4750
if assert_compilation:
4851
plot()
4952
code = tikzplotlib.get_tikz_code(
50-
include_disclaimer=False, standalone=True, **extra_get_tikz_code_args
53+
include_disclaimer=False,
54+
standalone=True,
55+
flavor=flavor,
56+
**extra_get_tikz_code_args
5157
)
5258
plt.close()
53-
assert _compile(code) is not None, code
59+
assert _compile(code, flavor) is not None, code
5460

5561

56-
def _compile(code):
62+
def _compile(code, flavor):
5763
_, tmp_base = tempfile.mkstemp()
5864

5965
tex_file = tmp_base + ".tex"
@@ -64,11 +70,12 @@ def _compile(code):
6470
os.chdir(os.path.dirname(tex_file))
6571

6672
# compile the output to pdf
73+
cmds = dict(
74+
latex=["pdflatex", "--interaction=nonstopmode"],
75+
context=["context", "--nonstopmode"],
76+
)
6777
try:
68-
subprocess.check_output(
69-
["pdflatex", "--interaction=nonstopmode", tex_file],
70-
stderr=subprocess.STDOUT,
71-
)
78+
subprocess.check_output(cmds[flavor] + [tex_file], stderr=subprocess.STDOUT)
7279
except subprocess.CalledProcessError as e:
7380
print("pdflatex output:")
7481
print("=" * 70)
@@ -81,15 +88,15 @@ def _compile(code):
8188
return output_pdf
8289

8390

84-
def compare_mpl_latex(plot):
91+
def compare_mpl_tex(plot, flavor="latex"):
8592
plot()
8693
code = tikzplotlib.get_tikz_code(standalone=True)
8794
directory = os.getcwd()
8895
filename = "test-0.png"
8996
plt.savefig(filename)
9097
plt.close()
9198

92-
pdf_file = _compile(code)
99+
pdf_file = _compile(code, flavor)
93100
pdf_dirname = os.path.dirname(pdf_file)
94101

95102
# Convert PDF to PNG.

test/test_annotate.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,5 @@ def test():
4444
if __name__ == "__main__":
4545
import helpers
4646

47-
helpers.compare_mpl_latex(plot)
47+
helpers.compare_mpl_tex(plot)
4848
# helpers.print_tree(plot())

test/test_arrows.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,5 +70,5 @@ def to_texstring(s):
7070
if __name__ == "__main__":
7171
import helpers
7272

73-
helpers.compare_mpl_latex(plot)
73+
helpers.compare_mpl_tex(plot)
7474
# helpers.print_tree(plot())

test/test_axvline.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@ def test():
2222
if __name__ == "__main__":
2323
import helpers
2424

25-
helpers.compare_mpl_latex(plot)
25+
helpers.compare_mpl_tex(plot)
2626
# helpers.print_tree(plot())

test/test_barchart_legend.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,5 @@ def test():
4040
if __name__ == "__main__":
4141
import helpers
4242

43-
helpers.compare_mpl_latex(plot)
43+
helpers.compare_mpl_tex(plot)
4444
# helpers.print_tree(plot())

test/test_basic_sin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,5 @@ def test():
2929
if __name__ == "__main__":
3030
import helpers
3131

32-
helpers.compare_mpl_latex(plot)
32+
helpers.compare_mpl_tex(plot)
3333
# helpers.print_tree(plot())

test/test_colorbars.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,5 +81,5 @@ def test():
8181
if __name__ == "__main__":
8282
import helpers
8383

84-
helpers.compare_mpl_latex(plot)
84+
helpers.compare_mpl_tex(plot)
8585
# helpers.print_tree(plot())

test/test_context.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from helpers import assert_equality
2+
3+
4+
def plot():
5+
import numpy as np
6+
import matplotlib.pyplot as plt
7+
import matplotlib.cm as cm
8+
9+
x, y = np.meshgrid(np.linspace(0, 1), np.linspace(0, 1))
10+
z = x ** 2 - y ** 2
11+
12+
fig = plt.figure()
13+
plt.pcolormesh(x, y, z, cmap=cm.viridis)
14+
15+
return fig
16+
17+
18+
def test():
19+
assert_equality(plot, __file__[:-3] + "_reference.tex", flavor="context")
20+
return

test/test_context_reference.tex

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
\starttikzpicture
2+
3+
\startaxis[
4+
tick align=outside,
5+
tick pos=left,
6+
x grid style={white!69.019608!black},
7+
xmin=0, xmax=1,
8+
xtick style={color=black},
9+
y grid style={white!69.019608!black},
10+
ymin=0, ymax=1,
11+
ytick style={color=black}
12+
]
13+
\addplot graphics [includegraphics cmd=\pgfimage,xmin=0, xmax=1, ymin=0, ymax=1] {tmp-000.png};
14+
\stopaxis
15+
16+
\stoptikzpicture

0 commit comments

Comments
 (0)