Save figures in matplotlib tests
See original GitHub issueMotivation
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:
- Created a year ago
- Comments:13 (7 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@nzw0301
It might be too late but we could add a fixture like below to save matplotlib figures after each test invocation:
One advantage of this approach is we can set
basetemp
when running tests and view how the saved figures look like:One risk is that this approach assumes that figures created in a test are not closed.
BytesIO
will be fine:Ref: https://github.com/matplotlib/matplotlib/blob/223b2b13d67f0be0e64898b9d3ca191f56dc6f82/lib/matplotlib/tests/test_backend_pdf.py