question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Save figures in matplotlib tests

See original GitHub issue

Motivation

As given in https://github.com/optuna/optuna/issues/3370, pytest cannot detect a broken figure by calling plot method, which means some test cases might not be covered in practice. One possible solution is to call plt.savefig(sys.stdout.buffer) as introduced in https://github.com/optuna/optuna/pull/3371.

Description

~Add plt.savefig(sts.std.out.buffuer) to matplotlib’s test functions:~ Add plt.savefig(io.BytesIO()) to matplotlib’s test functions:

  • test_edf.py (#3414)
  • test_parallel_coordinate.py (#3371)
  • test_slice.py (#3414)
  • test_intermediate_plot.py (#3414)
  • test_param_importances.py (#3414)
  • test_contour.py (#3414)
  • test_optimization_history.py (#3414)
  • test_pareto_front.py (#3414)

Alternatives (optional)

No response

Additional context (optional)

No response

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:13 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
harupycommented, Apr 4, 2022

@nzw0301

It might be too late but we could add a fixture like below to save matplotlib figures after each test invocation:


# tests/conftest.py
from pathlib import Path


import matplotlib.pyplot as plt
import pytest


@pytest.fixture(autouse=True)
def save_matplotlib_figure(request, tmp_path):
    yield
    # Save figures created in tests/visualization_tests/matplotlib_tests
    if (
        Path("tests", "visualization_tests", "matplotlib_tests").absolute()
        in Path(request.fspath).parenets
    ):
        for i in plt.get_fignums():
            fig = plt.figure(i)
            fig.savefig(tmp_path / f"figure_{i}.png")
            plt.close(fig)

One advantage of this approach is we can set basetemp when running tests and view how the saved figures look like:

pytest tests/visualization_tests/matplotlib_tests/test_parallel_coordinate.py --basetemp=ouptuts
image

One risk is that this approach assumes that figures created in a test are not closed.

1reaction
nzw0301commented, Mar 29, 2022
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Save a Plot to a File Using Matplotlib | Tutorial by Chartio
Learn how to save a plot to a file using Matplotlib, a plotting library for Python. In this tutorial, we'll show you to...
Read more >
matplotlib.pyplot.savefig — Matplotlib 3.6.2 documentation
Use a non-default backend to render the file, e.g. to render a png file with the "cairo" backend rather than the default "agg",...
Read more >
How to Save Plots To Image Files Using Matplotlib
In today's article we are going to showcase how to save matplotlib figures and plots into image files on your disk. Additionally, we...
Read more >
How to Save a Plot to a File Using Matplotlib? - GeeksforGeeks
Using the matplotlib.pyplot.imsave() method, we may save the plot to an image file rather than using Matplotlib to display it. The arrays are ......
Read more >
Save Plot as Image with Matplotlib - Stack Abuse
The savefig() function has a mandatory filename argument. Here, we've specified the filename and format. Additionally, it accepts other options, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found