Skip to content

Commit

Permalink
Merge pull request #12 from cgahr/v7
Browse files Browse the repository at this point in the history
Version 0.7.0
  • Loading branch information
cgahr authored Jul 2, 2023
2 parents 755b2ec + 1d8eea3 commit 2b53180
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 19 deletions.
16 changes: 16 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Change Log

## Version 0.7.0

### Highlights

This release exports all functions in `matplotlib.pyplot` to `latexplotlib`. This means you can use `lpl` as a drop-in replacement for `plt`and you don't have to import `matplotlib.pyplot` if you import latexplotlib. Also there is finally a changelog 🎉

### Code
- export `plt` functions in latexplotlib
- test new behavior and test that no functions beside `plt.subplots` are overridden

### Readme
- update to reflect `plt->lpl` changes
- add changelog
- updated example code to reflect new changes
19 changes: 7 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ pip install latexplotlib
```python
import latexplotlib as lpl

plt.style.use('latex10pt')
lpl.style.use('latex10pt')
# lpl.style.use('latex10pt-minimal')
```

3. replace all `plt.subplots` with `lpl.subplots`:
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)
Expand Down Expand Up @@ -70,7 +70,7 @@ import numpy as np

import latexplotlib as lpl

plt.style.use("latex10pt-minimal")
lpl.style.use("latex10pt-minimal")
# lpl.size.set(200, 400)
with lpl.size.context(200, 400):
fig, ax = lpl.subplots(1, 1)
Expand All @@ -86,8 +86,8 @@ ax.set_title("Perfect matplotlib figures for \\LaTeX")
ax.grid()

fig.legend()
plt.savefig("example_poly_minimal")
plt.savefig("example_poly_minimal.png")
fig.savefig("example_poly_minimal")
fig.savefig("example_poly_minimal.png")
```

<p align="center">
Expand All @@ -100,10 +100,9 @@ The non-minimal versions set additional defaults to create figures that are acce
```python
import matplotlib.pyplot as plt
import numpy as np
import latexplotlib as lpl


plt.style.use("latex10pt")
lpl.style.use("latex10pt")

# lpl.size.set(200, 400)
with lpl.size.context(200, 400):
Expand All @@ -130,7 +129,7 @@ fig.savefig("example_poly.png")
Both styles change the defaults of the `plt.savefig` command. The new defaults are

```python
plt.savefig(
lpl.savefig(
...,
bbox_inches=None,
dpi=300,
Expand Down Expand Up @@ -164,8 +163,6 @@ lpl.size() # (200, 400)

### Create figures for latex
```python
import matplotlib.pyplot as plt

import latexplotlib as lpl


Expand All @@ -183,8 +180,6 @@ fig, axes = lpl.subplots(1, 3, scale=0.8, aspect='equal')
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 matplotlib.pyplot as plt

import latexplotlib as lpl

# A 3 by 2 figure where each subplots height to width ratio is the golden ratio
Expand Down
3 changes: 1 addition & 2 deletions examples/example_poly.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import latexplotlib as lpl
import matplotlib.pyplot as plt
import numpy as np

plt.style.use("latex10pt")
lpl.style.use("latex10pt")

# lpl.size.set(200, 400)
with lpl.size.context(200, 400):
Expand Down
7 changes: 3 additions & 4 deletions examples/example_poly_minimal.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import latexplotlib as lpl
import matplotlib.pyplot as plt
import numpy as np

plt.style.use("latex10pt-minimal")
lpl.style.use("latex10pt-minimal")
# lpl.size.set(200, 400)
with lpl.size.context(200, 400):
fig, ax = lpl.subplots(1, 1)
Expand All @@ -18,5 +17,5 @@
ax.grid()

fig.legend()
plt.savefig("example_poly_minimal")
plt.savefig("example_poly_minimal.png")
fig.savefig("example_poly_minimal")
fig.savefig("example_poly_minimal.png")
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ tests = [
]

[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"

Expand Down
9 changes: 9 additions & 0 deletions src/latexplotlib/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Any

from ._cleanup import purge_old_styles
from ._config import size
from ._latexplotlib import (
Expand All @@ -18,5 +20,12 @@
"__version__",
]


def __getattr__(name: str) -> Any: # noqa: ANN401
import matplotlib.pyplot as plt

return getattr(plt, name)


purge_old_styles(__path__)
make_styles_available(__path__)
2 changes: 1 addition & 1 deletion src/latexplotlib/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.6.1"
__version__ = "0.7.0"
25 changes: 25 additions & 0 deletions tests/test_plt_available.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import latexplotlib as lpl
import matplotlib.pyplot as plt
import pytest


def test_plt_functions_available():
assert lpl.style == plt.style


@pytest.mark.parametrize("function", lpl._latexplotlib.__all__)
def test_lpl_functions_available(function: str):
assert getattr(lpl, function) == getattr(lpl._latexplotlib, function)


@pytest.mark.parametrize("function", lpl._latexplotlib.__all__)
def test_lpl_doesnt_overwrite_existing_in_plt(function):
if function == "subplots":
assert True
else:
assert function not in dir(plt)


def test_non_existent_raises():
with pytest.raises(AttributeError, match="matplotlib.pyplot"):
lpl.doesnt_exist # noqa: B018

0 comments on commit 2b53180

Please sign in to comment.