Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: ["3.9", "3.10", "3.11", "3.12"]
python-version: ["3.10", "3.11", "3.12", "3.13"]
runs-on: ubuntu-latest

steps:
Expand All @@ -45,7 +45,12 @@ jobs:
pip install -U pip
pip install .[dev]
- name: Run tests
run: pytest --mpl --verbose
run: pytest --mpl --verbose --mpl-generate-summary=html --mpl-results-path=./results
- uses: actions/upload-artifact@v4
if: failure()
with:
name: pytest-mpl-results-${{ matrix.python-version }}
path: ./results
- name: Run coverage
if: contains(env.USING_COVERAGE, matrix.python-version)
run: |
Expand Down
2 changes: 1 addition & 1 deletion mpl_template/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from .template import Template, insert_image

__version__ = "0.5.0"
__version__ = "0.5.1a"

__all__ = ["insert_image", "Template"]
33 changes: 15 additions & 18 deletions mpl_template/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ def insert_image(
relative to the given matplotlib.Axes object.
scale = 0.5 will scale the image to half of the
given matplotlib.Axes object.
dpi : int, optonal (default=300)
dpi : int, optional (default=300)
The Dots (pixel) Per Inch of the image.
expand : bool, optional (default=False)
If true, the image will expand to fill the axes
Expand Down Expand Up @@ -231,12 +231,7 @@ def insert_image(
if TAGS is None or Image is None: # pragma: no cover
raise ImportError("The `pillow` library is required to manipulate images.")

if "xticks" not in kwargs:
kwargs["xticks"] = []
if "yticks" not in kwargs:
kwargs["yticks"] = []
if "zorder" not in kwargs:
kwargs["zorder"] = 1
kwargs = {"xticks": [], "yticks": [], "zorder": 1} | kwargs

imgaxes = ax.figure.add_axes(ax.get_position(), **kwargs)
bbox = ax.get_window_extent().transformed(
Expand Down Expand Up @@ -579,7 +574,7 @@ def add_watermark(self, text=None):
text,
fontsize=24,
color="r",
fontname="Arial",
fontname="sans-serif",
fontweight="bold",
zorder=1000,
horizontalalignment="left",
Expand Down Expand Up @@ -685,16 +680,18 @@ def setup_figure(self) -> figure.Figure:

return self.fig

def blank(self) -> figure.Figure:
def blank(self, with_labels=True) -> figure.Figure:
self.add_frame()
for ax in self.add_titleblock():
ax.text(
0.5,
0.5,
'"{}"'.format(ax.get_label()),
va="center",
ha="center",
size=12,
)
axes = self.add_titleblock()
if with_labels:
for ax in axes:
ax.text(
0.5,
0.5,
'"{}"'.format(ax.get_label()),
va="center",
ha="center",
size=12,
)
self.watermark.remove()
return self.fig
Binary file modified mpl_template/tests/baseline_images/test_blank.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified mpl_template/tests/baseline_images/test_custom_spans.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified mpl_template/tests/baseline_images/test_custom_titleblock.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified mpl_template/tests/baseline_images/test_fancy_titleblock.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified mpl_template/tests/baseline_images/test_titleblock_on_left.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified mpl_template/tests/baseline_images/test_zero_margins.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
56 changes: 30 additions & 26 deletions mpl_template/tests/test_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

DEMO_PNG_URL = "https://raw.githubusercontent.com/austinorr/mpl-template/14496e1965e8b360093e0a559ae3f9aba6205a56/template/tests/img/polar_bar_demo.png"
DEMO_PNG_FILE = str(files("mpl_template.tests.img") / "polar_bar_demo.png")
IMG_TOL = 10
BASELINE_DIR = "baseline_images"
SCRIPTNAME = str(Path("mpl_template") / "tests" / "test_template.py")

Expand All @@ -31,7 +30,7 @@ def test_calc_extents(size, scale, expected):
@pytest.mark.parametrize("i", range(4))
@pytest.mark.mpl_image_compare(
baseline_dir=BASELINE_DIR,
tolerance=IMG_TOL,
tolerance=3,
filename="grace_hopper.png",
savefig_kwargs={"dpi": 96},
)
Expand All @@ -47,16 +46,19 @@ def test__apply_exif_rotation(i):
return fig


@pytest.mark.mpl_image_compare(baseline_dir=BASELINE_DIR, tolerance=IMG_TOL)
@pytest.mark.mpl_image_compare(
baseline_dir=BASELINE_DIR,
tolerance=4, # higher tolerance since this one includes freetype text
)
def test_blank():
testfig = template.Template(figsize=(8.5, 11), scriptname="")
testfig = template.Template(figsize=(5, 3), scriptname="")
testfig.path_text = SCRIPTNAME
_ = testfig.blank()

return testfig.fig


@pytest.mark.mpl_image_compare(baseline_dir=BASELINE_DIR, tolerance=IMG_TOL)
@pytest.mark.mpl_image_compare(baseline_dir=BASELINE_DIR, remove_text=True)
def test_insert_image_from_url():
url = DEMO_PNG_URL
fig, ax = plt.subplots(figsize=(9, 9))
Expand All @@ -71,31 +73,31 @@ def test_insert_svg_image_from_url():
_ = template.insert_image(ax, url, scale=1)


@pytest.mark.mpl_image_compare(baseline_dir=BASELINE_DIR, tolerance=IMG_TOL)
@pytest.mark.mpl_image_compare(baseline_dir=BASELINE_DIR, remove_text=True)
def test_insert_image_from_url_shrink_half():
url = DEMO_PNG_URL
fig, ax = plt.subplots(figsize=(9, 9))
_ = template.insert_image(ax, url, scale=0.5)
return fig


@pytest.mark.mpl_image_compare(baseline_dir=BASELINE_DIR, tolerance=IMG_TOL)
@pytest.mark.mpl_image_compare(baseline_dir=BASELINE_DIR, remove_text=True)
def test_insert_image_from_url_zoom_10x():
url = DEMO_PNG_URL
fig, ax = plt.subplots(figsize=(9, 9))
_ = template.insert_image(ax, url, scale=10)
return fig


@pytest.mark.mpl_image_compare(baseline_dir=BASELINE_DIR, tolerance=IMG_TOL)
@pytest.mark.mpl_image_compare(baseline_dir=BASELINE_DIR, remove_text=True)
def test_insert_image_from_url_zoom_10x_expand():
url = DEMO_PNG_URL
fig, ax = plt.subplots(figsize=(9, 9))
_ = template.insert_image(ax, url, scale=10, expand=True)
return fig


@pytest.mark.mpl_image_compare(baseline_dir=BASELINE_DIR, tolerance=IMG_TOL)
@pytest.mark.mpl_image_compare(baseline_dir=BASELINE_DIR, remove_text=True)
def test_insert_image_from_file():
file = DEMO_PNG_FILE

Expand All @@ -104,7 +106,7 @@ def test_insert_image_from_file():
return fig


@pytest.mark.mpl_image_compare(baseline_dir=BASELINE_DIR, tolerance=IMG_TOL)
@pytest.mark.mpl_image_compare(baseline_dir=BASELINE_DIR, remove_text=True)
def test_insert_image_from_file_shrink_half():
file = DEMO_PNG_FILE

Expand All @@ -113,7 +115,7 @@ def test_insert_image_from_file_shrink_half():
return fig


@pytest.mark.mpl_image_compare(baseline_dir=BASELINE_DIR, tolerance=IMG_TOL)
@pytest.mark.mpl_image_compare(baseline_dir=BASELINE_DIR, remove_text=True)
def test_insert_image_from_file_zoom_10x():
file = DEMO_PNG_FILE

Expand All @@ -122,7 +124,7 @@ def test_insert_image_from_file_zoom_10x():
return fig


@pytest.mark.mpl_image_compare(baseline_dir=BASELINE_DIR, tolerance=IMG_TOL)
@pytest.mark.mpl_image_compare(baseline_dir=BASELINE_DIR, remove_text=True)
def test_insert_image_from_file_zoom_10x_expand():
file = DEMO_PNG_FILE

Expand All @@ -132,7 +134,7 @@ def test_insert_image_from_file_zoom_10x_expand():


@pytest.mark.mpl_image_compare(
baseline_dir=BASELINE_DIR, tolerance=IMG_TOL, filename="test_custom_spans.png"
baseline_dir=BASELINE_DIR, filename="test_custom_spans.png"
)
@pytest.mark.parametrize("base", [None, 10])
def test_custom_spans(base):
Expand All @@ -149,13 +151,13 @@ def test_custom_spans(base):
figsize=(5, 3), scriptname="", titleblock_content=test, base=base
)
testfig.path_text = SCRIPTNAME
_ = testfig.blank()
_ = testfig.blank(with_labels=False)

return testfig.fig


@pytest.mark.mpl_image_compare(
baseline_dir=BASELINE_DIR, tolerance=IMG_TOL, filename="test_custom_spans.png"
baseline_dir=BASELINE_DIR, filename="test_custom_spans.png"
)
def test_custom_spans_100():
test = [
Expand All @@ -171,36 +173,35 @@ def test_custom_spans_100():
figsize=(5, 3), scriptname="", titleblock_content=test, base=100
)
testfig.path_text = SCRIPTNAME
_ = testfig.blank()
_ = testfig.blank(with_labels=False)

return testfig.fig


@pytest.mark.mpl_image_compare(
baseline_dir=BASELINE_DIR,
tolerance=IMG_TOL,
filename="test_titleblock_on_left.png",
)
@pytest.mark.parametrize("base", [None, 10, 100])
def test_titleblock_on_left(base):
testfig = template.Template(figsize=(8.5, 11), scriptname="", base=base)
testfig.gstitleblock = testfig.gsfig[
-(testfig.bottom + testfig.t_h) or None : -testfig.bottom or None,
(testfig.left) or None : -(testfig.left + testfig.t_w) or None,
(testfig.left) or None : (testfig.left + testfig.t_w) or None,
]

_ = testfig.blank()
_ = testfig.blank(with_labels=False)

return testfig.fig


@pytest.mark.mpl_image_compare(baseline_dir=BASELINE_DIR, tolerance=IMG_TOL)
@pytest.mark.mpl_image_compare(baseline_dir=BASELINE_DIR)
def test_zero_margins():
testfig = template.Template(
figsize=(8.5, 11), scriptname="tests.py", margins=(0, 0, 0, 0)
figsize=(5, 3), scriptname="tests.py", margins=(0, 0, 0, 0)
)

_ = testfig.blank()
_ = testfig.blank(with_labels=False)

return testfig.fig

Expand All @@ -219,7 +220,10 @@ def test_bad_margins(bad_margin):
)


@pytest.mark.mpl_image_compare(baseline_dir=BASELINE_DIR, tolerance=IMG_TOL)
@pytest.mark.mpl_image_compare(
baseline_dir=BASELINE_DIR,
tolerance=8, # increase tolerance for fonts
)
def test_custom_titleblock():
custom = [
{
Expand Down Expand Up @@ -267,7 +271,7 @@ def test_custom_titleblock():
]

testfig = template.Template(
figsize=(8.5, 11), scriptname="", titleblock_content=custom
figsize=(5, 3), scriptname="", titleblock_content=custom
)

testfig.path_text = SCRIPTNAME
Expand All @@ -279,7 +283,7 @@ def test_custom_titleblock():
@pytest.mark.parametrize("base", [None, 10, 100])
@pytest.mark.mpl_image_compare(
baseline_dir=BASELINE_DIR,
tolerance=IMG_TOL,
tolerance=8, # increase tolerance for fonts
filename="test_fancy_titleblock.png",
)
def test_fancy_titleblock(base):
Expand Down Expand Up @@ -329,7 +333,7 @@ def test_fancy_titleblock(base):
]

testfig = template.Template(
figsize=(8.5, 11), scriptname="", titleblock_content=fancy, base=base
figsize=(5, 3), scriptname="", titleblock_content=fancy, base=base
)
testfig.path_text = SCRIPTNAME
_ = testfig.setup_figure()
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ build-backend = "setuptools.build_meta"
name = "mpl_template"
description = "mpl-template: matplotlib report template constructor"
readme = "README.md"
requires-python = ">=3.9"
requires-python = ">=3.10"
license = { text = "BSD-3-Clause" }
authors = [{ name = "Austin Orr", email = "[email protected]" }]
classifiers = [
Expand All @@ -15,7 +15,6 @@ classifiers = [
"Programming Language :: Python",
"Intended Audience :: Science/Research",
"Programming Language :: Python :: 3 :: Only",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
Expand All @@ -27,6 +26,7 @@ dynamic = ["version"]
all = ["requests", "pillow"]
dev = [
"mpl-template[all]",
"matplotlib>=3.10,<3.11", # pins mpl & freetype version
"coverage>=6.0.0",
"pytest>=7.0.0",
"pytest-cov>=4.1",
Expand Down