百科.dev
全部条目AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
T

tikzplotlib

> 编程语言
开源

:bar_chart: 将 matplotlib 图像保存为 TikZ/PGFplots,以便与 LaTeX 进行平滑集成。

2.6K stars0 点赞0 次浏览
访问官网GitHub

工具介绍

:bar_chart: 将 matplotlib 图像保存为 TikZ/PGFplots,以便与 LaTeX 进行平滑集成。

The artist formerly known as matplotlib2tikz.

This is tikzplotlib, a Python tool for converting matplotlib figures into [PGFPlots](https://www.ctan.org/pkg/pgfplots) ([PGF/TikZ](https://www.ctan.org/pkg/pgf)) figures like for native inclusion into LaTeX or ConTeXt documents. The output of tikzplotlib is in [PGFPlots](https://github.com/pgf-tikz/pgfplots/), a TeX library that sits on top of [PGF/TikZ](https://en.wikipedia.org/wiki/PGF/TikZ) and describes graphs in terms of axes, data etc. Consequently, the output of tikzplotlib - retains more information, - can be more easily understood, and - is more easily editable than [raw TikZ output](https://matplotlib.org/users/whats_new.html#pgf-tikz-backend). For example, the matplotlib figure ```python import matplotlib.pyplot as plt import numpy as np plt.style.use("ggplot") t = np.arange(0.0, 2.0, 0.1) s = np.sin(2 * np.pi * t) s2 = np.cos(2 * np.pi * t) plt.plot(t, s, "o-", lw=4.1) plt.plot(t, s2, "o-", lw=4.1) plt.xlabel("time (s)") plt.ylabel("Voltage (mV)") plt.title("Simple plot $\\frac{\\alpha}{2}$") plt.grid(True) import tikzplotlib tikzplotlib.save("test.tex") ``` ```python import matplotlib as mpl plt.close() mpl.rcParams.update(mpl.rcParamsDefault) ``` --> (see above) gives ``` … ``` (Use `get_tikz_code()` instead of `save()` if you want the code as a string.) Tweaking the plot is straightforward and can be done as part of your TeX work flow. [The fantastic PGFPlots manual](http://pgfplots.sourceforge.net/pgfplots.pdf) contains great examples of how to make your plot look even better. Of course, not all figures produced by matplotlib can be converted without error. Notably, [3D plots don't work](https://github.com/matplotlib/matplotlib/issues/7243). ### Installation tikzplotlib is [available from the Python Package Index](https://pypi.org/project/tikzplotlib/), so simply do ``` pip install tikzplotlib ``` to install. ### Usage 1. Generate your matplotlib plot as usual. 2. Instead of `pyplot.show()`, invoke tikzplotlib by ```python import tikzplotlib tikzplotlib.save("mytikz.tex") # or tikzplotlib.save("mytikz.tex", flavor="context") ``` to store the TikZ file as `mytikz.tex`. 3. Add the contents of `mytikz.tex` into your TeX source code. A convenient way of doing so is via ```latex \input{/path/to/mytikz.tex} ``` Also make sure that the packages for PGFPlots and proper Unicode support and are included in the header of your document: ```latex \\usepackage[utf8]{inputenc} \\usepackage{pgfplots} \DeclareUnicodeCharacter{2212}{−} \\usepgfplotslibrary{groupplots,dateplot} \\usetikzlibrary{patterns,shapes.arrows} \pgfplotsset{compat=newest} ``` or: ```latex \setupcolors[state=start] \\usemodule[tikz] \\usemodule[pgfplots] \\usepgfplotslibrary[groupplots,dateplot] \\usetikzlibrary[patterns,shapes.arrows] \pgfplotsset{compat=newest} \\unexpanded\def\startgroupplot{\groupplot} \\unexpanded\def\stopgroupplot{\endgroupplot} ``` You can also get the code via: ```python import tikzplotlib tikzplotlib.Flavors.latex.preamble() # or tikzplotlib.Flavors.context.preamble() ``` 4. [Optional] Clean up the figure before exporting to tikz using the `clean_figure` command. ```python import matplotlib.pyplot as plt import numpy as np # ... do your plotting import tikzplotlib tikzplotlib.clean_figure() tikzplotlib.save("test.tex") ``` The command will remove points that are outside the axes limits, simplify curves and reduce point density for the specified target resolution. The feature originated from the [matlab2tikz](https://github.com/matlab2tikz/matlab2tikz) project and is adapted to matplotlib. ### Contributing If you experience bugs, would like to contribute, have nice examples of what tikzplotlib can do, or if you are just looking for more information, then please visit [tikzplotlib's GitHub page](https://github.com/nschloe/tikzplotlib). ### Testing tikzplotlib has automatic unit testing to make sure that the software doesn't accidentally get worse over time. In `test/`, a number of test cases are specified. Those run through tikzplotlib and compare the output with a previously stored reference TeX file. To run the tests, just check out this repository and type ``` pytest ``` ### License tikzplotlib is published under the [MIT license](https://en.wikipedia.org/wiki/MIT_License).

GitHub Issues· 0 开放

在 GitHub 查看全部

暂无开放 Issues,或尚未同步最近议题。

核心特点

  • •retains more information,
  • •can be more easily understood, and
  • •is more easily editable

> 标签

Pythonlatexmatplotlibpgfplotspython

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言