# latexplotlib
[![image](https://img.shields.io/pypi/v/latexplotlib.svg)](https://pypi.python.org/pypi/latexplotlib)
[![image](https://img.shields.io/pypi/l/latexplotlib.svg)](https://pypi.python.org/pypi/latexplotlib)
[![image](https://img.shields.io/pypi/pyversions/latexplotlib.svg)](https://pypi.python.org/pypi/latexplotlib)
[![Actions status](https://github.com/cgahr/latexplotlib/actions/workflows/main.yml/badge.svg)](https://github.com/cgahr/latexplotlib/actions)
[![Coverage Status](https://coveralls.io/repos/github/cgahr/latexplotlib/badge.svg?branch=main)](https://coveralls.io/github/cgahr/latexplotlib?branch=main)
[![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)
Perfect matplotlib figures for latex.
## Quickstart
1. install `latexplotlib`:
```python
pip install latexplotlib
```
2. import latexplotlib and use latexplotlib style
```python
import latexplotlib as lpl
lpl.style.use('latex10pt')
# lpl.style.use('latex10pt-minimal')
```
3. replace all usages of `plt` with `lpl`. Only `plt.subplots` changes its behavior:
```python
# fig, axes = plt.subplots(2, 3)
fig, axes = lpl.subplots(2, 3)
```
Optional:
4. get size of latex document
```latex
(\the\textwidth, \the\textheight) % (412.123pt, 346.564pt)
```
5. set `lpl.size` to size of latex document
```python
lpl.size.set(412.123, 346.564)
```
## Usage
This package has two basic functionalities. On the one hand, it sets sensible defaults
for creating perfect figures for latex. This includes a color scheme optimized for
color-blind people, correct font and font sizes, and sensible defaults to store the
figure. On the other hand, it provides some functions to create perfectly sized figures.
These figures fit your latex document without scaling and have the correct font size for
your document.
### latexplotlib styles
There are 6 different styles for matplotlib:
- `latex10pt-minimal`
- `latex11pt-minimal`
- `latex12pt-minimal`
- `latex10pt`
- `latex11pt`
- `latex12pt`
The `*minimal` versions change the font and the font sizes to ensure that the figures fonts match the latex font. This style is fully compatible with other styles:
```python
import matplotlib.pyplot as plt
import numpy as np
import latexplotlib as lpl
lpl.style.use("latex10pt-minimal")
# lpl.size.set(200, 400)
with lpl.size.context(200, 400):
fig, ax = lpl.subplots(1, 1)
x = np.linspace(1, 5, 100)
for t in range(4):
label = f"$x^{t}$"
ax.plot(x, x ** t, label=label)
ax.set_yscale("log")
ax.set_title("Perfect matplotlib figures for \\LaTeX")
ax.grid()
fig.legend()
fig.savefig("example_poly_minimal")
fig.savefig("example_poly_minimal.png")
```
<p align="center">
<img src="https://github.com/ConstantinGahr/latexplotlib/blob/main/examples/example_poly_minimal.png?raw=true" width="500">
</p>
The non-minimal versions set additional defaults to create figures that are accessible for color-blind people:
```python
import matplotlib.pyplot as plt
import numpy as np
lpl.style.use("latex10pt")
# lpl.size.set(200, 400)
with lpl.size.context(200, 400):
fig, ax = lpl.subplots(1, 1)
x = np.linspace(1, 5, 100)
for t in range(4):
label = f"$x^{t}$"
ax.plot(x, x ** t, label=label)
ax.set_yscale("log")
ax.set_title("Perfect matplotlib figures for \\LaTeX")
ax.grid()
fig.legend()
fig.savefig("example_poly")
fig.savefig("example_poly.png")
```
<p align="center">
<img src="https://github.com/ConstantinGahr/latexplotlib/blob/main/examples/example_poly.png?raw=true" width="500">
</p>
Both styles change the defaults of the `plt.savefig` command. The new defaults are
```python
lpl.savefig(
...,
bbox_inches=None,
dpi=300,
format="pdf",
orientation="portrait",
pad_inches=0.05
)
```
### Get latex dimensions
You can find the width and height of your document using the following command:
```latex
\the\textwidth
\the\textheight
```
### Set and get latex page size
```python
import latexplotlib as lpl
lpl.size.set(200, 400)
with lpl.size.context(100, 200):
lpl.size() # 100, 200
lpl.size() # (200, 400)
```
### Create figures for latex
```python
import latexplotlib as lpl
# A figure filling 75% of the latex page
_ = lpl.subplots(1, 1)
# A subplot filling 80% of the latex page
fig, axes = lpl.subplots(3, 2, scale=0.8)
# A subplot for 3 square plots next to each other
fig, axes = lpl.subplots(1, 3, scale=0.8, aspect='equal')
```
### `aspect` keyword
The `aspect` keyword controls the ratio of height to width. The default is the Golden ratio. `aspect` can also be `equal` (i.e. `aspect=1` )or `auto`. In the latter case, the figure fills the available space.
```python
import latexplotlib as lpl
# A 3 by 2 figure where each subplots height to width ratio is the golden ratio
fig, axes = lpl.subplots(3, 2)
# A 3 by 2 figure where each subplot having a height to width ratio of 1:1
fig, axes = lpl.subplots(3, 2, aspect=1.0)
# A figure that is exactly 300pt height and 200pt wide
with lpl.size.context(200, 300):
fig, axes = lpl.subplots(3, 2, aspect="auto")
```
### Include figures in Latex
The most important part of including the figures in latex is to not set the size of the figure using arguments like `[width=...]`:
```latex
\includegraphics{test.pdf}
```
Observe that we did NOT adjust the size using arguments like `[width=...]`:
```latex
\includegraphics[width=\textwidth]{test.pdf}
```
### `plt.tight_layout()`
`plt.tight_layout()` changes the size of the produced figure. As such it is recommended to not use `plt.tight_layout()` at all! The same is true for `savefig(..., bbox_inches=None)`!
Instead all latexplotlib styles used `constrained_layout` by default. `constrained_layout` has a similar functionality compared to `tight_layout`, however it is fully deterministic and does not change the size of the underlying figure.
## References
This package is inspired by the following sources:
- Code: https://pypi.org/project/SciencePlots/
- Figure sizes: https://jwalton.info/Embed-Publication-Matplotlib-Latex/
- Color palette (Okabe Ito): https://clauswilke.com/dataviz/color-basics.html
- Golden ratio: https://en.wikipedia.org/wiki/Golden_ratio
Raw data
{
"_id": null,
"home_page": null,
"name": "latexplotlib",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.8",
"maintainer_email": null,
"keywords": "latex, matplotlib-figures, matplotlib-style-sheets, matplotlib-styles, python",
"author": null,
"author_email": "Constantin Gahr <latexplotlib.gvxel@aleeas.com>",
"download_url": "https://files.pythonhosted.org/packages/13/35/fa4d99d937a43a4e791b586f78e1c3c5263c0bb498e7f1def666a817840d/latexplotlib-0.8.3.tar.gz",
"platform": null,
"description": "# latexplotlib\n\n[![image](https://img.shields.io/pypi/v/latexplotlib.svg)](https://pypi.python.org/pypi/latexplotlib)\n[![image](https://img.shields.io/pypi/l/latexplotlib.svg)](https://pypi.python.org/pypi/latexplotlib)\n[![image](https://img.shields.io/pypi/pyversions/latexplotlib.svg)](https://pypi.python.org/pypi/latexplotlib)\n[![Actions status](https://github.com/cgahr/latexplotlib/actions/workflows/main.yml/badge.svg)](https://github.com/cgahr/latexplotlib/actions)\n[![Coverage Status](https://coveralls.io/repos/github/cgahr/latexplotlib/badge.svg?branch=main)](https://coveralls.io/github/cgahr/latexplotlib?branch=main)\n[![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)\n\nPerfect matplotlib figures for latex.\n\n\n## Quickstart\n\n1. install `latexplotlib`:\n```python\npip install latexplotlib\n```\n2. import latexplotlib and use latexplotlib style\n```python\nimport latexplotlib as lpl\n\nlpl.style.use('latex10pt')\n# lpl.style.use('latex10pt-minimal')\n```\n\n3. replace all usages of `plt` with `lpl`. Only `plt.subplots` changes its behavior:\n```python\n# fig, axes = plt.subplots(2, 3)\nfig, axes = lpl.subplots(2, 3)\n```\n\nOptional:\n\n4. get size of latex document\n```latex\n(\\the\\textwidth, \\the\\textheight) % (412.123pt, 346.564pt)\n```\n5. set `lpl.size` to size of latex document\n```python\nlpl.size.set(412.123, 346.564)\n```\n\n## Usage\n\nThis package has two basic functionalities. On the one hand, it sets sensible defaults\nfor creating perfect figures for latex. This includes a color scheme optimized for\ncolor-blind people, correct font and font sizes, and sensible defaults to store the\nfigure. On the other hand, it provides some functions to create perfectly sized figures.\nThese figures fit your latex document without scaling and have the correct font size for\nyour document.\n\n### latexplotlib styles\n\nThere are 6 different styles for matplotlib:\n\n- `latex10pt-minimal`\n- `latex11pt-minimal`\n- `latex12pt-minimal`\n- `latex10pt`\n- `latex11pt`\n- `latex12pt`\n\nThe `*minimal` versions change the font and the font sizes to ensure that the figures fonts match the latex font. This style is fully compatible with other styles:\n\n```python\nimport matplotlib.pyplot as plt\nimport numpy as np\n\nimport latexplotlib as lpl\n\nlpl.style.use(\"latex10pt-minimal\")\n# lpl.size.set(200, 400)\nwith lpl.size.context(200, 400):\n fig, ax = lpl.subplots(1, 1)\n\nx = np.linspace(1, 5, 100)\n\nfor t in range(4):\n label = f\"$x^{t}$\"\n ax.plot(x, x ** t, label=label)\n\nax.set_yscale(\"log\")\nax.set_title(\"Perfect matplotlib figures for \\\\LaTeX\")\nax.grid()\n\nfig.legend()\nfig.savefig(\"example_poly_minimal\")\nfig.savefig(\"example_poly_minimal.png\")\n```\n\n<p align=\"center\">\n<img src=\"https://github.com/ConstantinGahr/latexplotlib/blob/main/examples/example_poly_minimal.png?raw=true\" width=\"500\">\n</p>\n\n\nThe non-minimal versions set additional defaults to create figures that are accessible for color-blind people:\n\n```python\nimport matplotlib.pyplot as plt\nimport numpy as np\n\n\nlpl.style.use(\"latex10pt\")\n\n# lpl.size.set(200, 400)\nwith lpl.size.context(200, 400):\n fig, ax = lpl.subplots(1, 1)\n\nx = np.linspace(1, 5, 100)\n\nfor t in range(4):\n label = f\"$x^{t}$\"\n ax.plot(x, x ** t, label=label)\n\nax.set_yscale(\"log\")\nax.set_title(\"Perfect matplotlib figures for \\\\LaTeX\")\nax.grid()\n\nfig.legend()\nfig.savefig(\"example_poly\")\nfig.savefig(\"example_poly.png\")\n```\n<p align=\"center\">\n<img src=\"https://github.com/ConstantinGahr/latexplotlib/blob/main/examples/example_poly.png?raw=true\" width=\"500\">\n</p>\n\nBoth styles change the defaults of the `plt.savefig` command. The new defaults are\n\n```python\nlpl.savefig(\n ...,\n bbox_inches=None,\n dpi=300,\n format=\"pdf\",\n orientation=\"portrait\",\n pad_inches=0.05\n)\n```\n\n### Get latex dimensions\nYou can find the width and height of your document using the following command:\n\n```latex\n\\the\\textwidth\n\n\\the\\textheight\n```\n\n### Set and get latex page size\n\n```python\nimport latexplotlib as lpl\n\nlpl.size.set(200, 400)\n\nwith lpl.size.context(100, 200):\n lpl.size() # 100, 200\n\nlpl.size() # (200, 400)\n```\n\n### Create figures for latex\n```python\nimport latexplotlib as lpl\n\n\n# A figure filling 75% of the latex page\n_ = lpl.subplots(1, 1)\n\n# A subplot filling 80% of the latex page\nfig, axes = lpl.subplots(3, 2, scale=0.8)\n\n# A subplot for 3 square plots next to each other\nfig, axes = lpl.subplots(1, 3, scale=0.8, aspect='equal')\n```\n\n### `aspect` keyword\nThe `aspect` keyword controls the ratio of height to width. The default is the Golden ratio. `aspect` can also be `equal` (i.e. `aspect=1` )or `auto`. In the latter case, the figure fills the available space.\n\n```python\nimport latexplotlib as lpl\n\n# A 3 by 2 figure where each subplots height to width ratio is the golden ratio\nfig, axes = lpl.subplots(3, 2)\n\n# A 3 by 2 figure where each subplot having a height to width ratio of 1:1\nfig, axes = lpl.subplots(3, 2, aspect=1.0)\n\n# A figure that is exactly 300pt height and 200pt wide\nwith lpl.size.context(200, 300):\n fig, axes = lpl.subplots(3, 2, aspect=\"auto\")\n```\n\n\n### Include figures in Latex\n\nThe most important part of including the figures in latex is to not set the size of the figure using arguments like `[width=...]`:\n```latex\n\\includegraphics{test.pdf}\n```\nObserve that we did NOT adjust the size using arguments like `[width=...]`:\n```latex\n\\includegraphics[width=\\textwidth]{test.pdf}\n```\n\n### `plt.tight_layout()`\n\n`plt.tight_layout()` changes the size of the produced figure. As such it is recommended to not use `plt.tight_layout()` at all! The same is true for `savefig(..., bbox_inches=None)`!\n\nInstead all latexplotlib styles used `constrained_layout` by default. `constrained_layout` has a similar functionality compared to `tight_layout`, however it is fully deterministic and does not change the size of the underlying figure.\n\n## References\n\nThis package is inspired by the following sources:\n\n- Code: https://pypi.org/project/SciencePlots/\n- Figure sizes: https://jwalton.info/Embed-Publication-Matplotlib-Latex/\n- Color palette (Okabe Ito): https://clauswilke.com/dataviz/color-basics.html\n- Golden ratio: https://en.wikipedia.org/wiki/Golden_ratio\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Perfect matplotlib figures for latex",
"version": "0.8.3",
"project_urls": {
"Changelog": "https://github.com/cgahr/latexplotlib/blob/main/CHANGES.md",
"Homepage": "https://github.com/cgahr/latexplotlib",
"Issues": "https://github.com/cgahr/latexplotlib/issues"
},
"split_keywords": [
"latex",
" matplotlib-figures",
" matplotlib-style-sheets",
" matplotlib-styles",
" python"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "1335fa4d99d937a43a4e791b586f78e1c3c5263c0bb498e7f1def666a817840d",
"md5": "2b89deeb657492e58498978b27efc849",
"sha256": "ccda8ee8c483bf7322a738545e6f7c99ca18e75e5045f395e4632f37a921ee74"
},
"downloads": -1,
"filename": "latexplotlib-0.8.3.tar.gz",
"has_sig": false,
"md5_digest": "2b89deeb657492e58498978b27efc849",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.8",
"size": 17454,
"upload_time": "2024-08-07T13:21:15",
"upload_time_iso_8601": "2024-08-07T13:21:15.523982Z",
"url": "https://files.pythonhosted.org/packages/13/35/fa4d99d937a43a4e791b586f78e1c3c5263c0bb498e7f1def666a817840d/latexplotlib-0.8.3.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-08-07 13:21:15",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "cgahr",
"github_project": "latexplotlib",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"requirements": [],
"lcname": "latexplotlib"
}