@@ -31,12 +31,13 @@ The output of tikzplotlib is in
31
31
top of [ PGF/TikZ] ( https://en.wikipedia.org/wiki/PGF/TikZ ) and describes graphs in terms
32
32
of axes, data etc. Consequently, the output of tikzplotlib
33
33
34
- * retains more information,
35
- * can be more easily understood, and
36
- * is more easily editable
34
+ - retains more information,
35
+ - can be more easily understood, and
36
+ - is more easily editable
37
37
38
38
than [ raw TikZ output] ( https://matplotlib.org/users/whats_new.html#pgf-tikz-backend ) .
39
39
For example, the matplotlib figure
40
+
40
41
``` python
41
42
import matplotlib.pyplot as plt
42
43
import numpy as np
@@ -57,16 +58,20 @@ import tikzplotlib
57
58
58
59
tikzplotlib.save(" test.tex" )
59
60
```
61
+
60
62
<!-- close the figure and reset defaults
61
63
<!--pytest-codeblocks:cont-->
64
+
62
65
``` python
63
66
import matplotlib as mpl
64
67
65
68
plt.close()
66
69
mpl.rcParams.update(mpl.rcParamsDefault)
67
70
```
71
+
68
72
-->
69
73
(see above) gives
74
+
70
75
``` latex
71
76
\begin{tikzpicture}
72
77
@@ -108,6 +113,7 @@ table {%
108
113
109
114
\end{tikzpicture}
110
115
```
116
+
111
117
(Use ` get_tikz_code() ` instead of ` save() ` if you want the code as a string.)
112
118
113
119
Tweaking the plot is straightforward and can be done as part of your TeX work flow.
@@ -121,33 +127,39 @@ Notably, [3D plots don't work](https://github.com/matplotlib/matplotlib/issues/7
121
127
122
128
tikzplotlib is [ available from the Python Package
123
129
Index] ( https://pypi.org/project/tikzplotlib/ ) , so simply do
130
+
124
131
```
125
132
pip install tikzplotlib
126
133
```
127
- to install.
128
134
135
+ to install.
129
136
130
137
### Usage
131
138
132
139
1 . Generate your matplotlib plot as usual.
133
140
134
141
2 . Instead of ` pyplot.show() ` , invoke tikzplotlib by
142
+
135
143
``` python
136
144
import tikzplotlib
137
145
138
146
tikzplotlib.save(" mytikz.tex" )
139
147
# or
140
148
tikzplotlib.save(" mytikz.tex" , flavor = " context" )
141
149
```
142
- to store the TikZ file as `mytikz.tex` .
150
+
151
+ to store the TikZ file as `mytikz.tex` .
143
152
144
153
3 . Add the contents of `mytikz.tex` into your TeX source code. A convenient way of
145
154
doing so is via
155
+
146
156
```latex
147
157
\input{/path/to/mytikz.tex}
148
158
```
149
- Also make sure that the packages for PGFPlots and proper Unicode support and are
150
- included in the header of your document:
159
+
160
+ Also make sure that the packages for PGFPlots and proper Unicode support and are
161
+ included in the header of your document:
162
+
151
163
```latex
152
164
\usepackage[utf8]{inputenc}
153
165
\usepackage{pgfplots}
@@ -156,7 +168,9 @@ to install.
156
168
\usetikzlibrary{patterns,shapes.arrows}
157
169
\pgfplotsset{compat=newest}
158
170
```
159
- or :
171
+
172
+ or :
173
+
160
174
```latex
161
175
\setupcolors[state=start]
162
176
\usemodule[tikz]
@@ -167,7 +181,9 @@ to install.
167
181
\unexpanded\def\startgroupplot{\groupplot}
168
182
\unexpanded\def\stopgroupplot{\endgroupplot}
169
183
```
170
- You can also get the code via:
184
+
185
+ You can also get the code via:
186
+
171
187
```python
172
188
import tikzplotlib
173
189
@@ -177,6 +193,7 @@ to install.
177
193
```
178
194
179
195
4 . Optional: clean up the figure before exporting to tikz using the `clean_figure` command.
196
+
180
197
```python
181
198
import matplotlib.pyplot as plt
182
199
import numpy as np
@@ -188,9 +205,10 @@ to install.
188
205
tikzplotlib.clean_figure()
189
206
tikzplotlib.save(" test.tex" )
190
207
```
208
+
191
209
The command will remove points that are outside the axes limits, simplify curves and
192
210
reduce point density for the specified target resolution.
193
-
211
+
194
212
The feature originated from the
195
213
[matlab2tikz](https:// github.com/ matlab2tikz/ matlab2tikz) project and is adapted to
196
214
matplotlib.
@@ -201,7 +219,6 @@ If you experience bugs, would like to contribute, have nice examples of what
201
219
tikzplotlib can do, or if you are just looking for more information, then please
202
220
visit [tikzplotlib' s GitHub page](https://github.com/nschloe/tikzplotlib).
203
221
204
-
205
222
# ## Testing
206
223
207
224
tikzplotlib has automatic unit testing to make sure that the software doesn' t
@@ -210,6 +227,7 @@ Those run through tikzplotlib and compare the output with a previously stored
210
227
reference TeX file .
211
228
212
229
To run the tests, just check out this repository and type
230
+
213
231
```
214
232
pytest
215
233
```
0 commit comments